check_port() { echo"Checking instance port ..." netstat -tlpn | grep "\b$1\b" } if check_port 3389 then echo"PC NOW UP" >> /root/PC_STATUS TZ=UTC-8 date +"%Y-%m-%d %H:%M:%S" >> /root/PC_STATUS exit 1 else curl
定时任务
1
*/10 * * * * /root/check.sh
每隔十分钟执行一次下面的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/bash
check_port() { echo"Checking instance port ..." netstat -tlpn | grep "\b$1\b" } if check_port 3389 then echo"PC NOW UP" >> /root/PC_STATUS TZ=UTC-8 date +"%Y-%m-%d %H:%M:%S" >> /root/PC_STATUS exit 1 else curl 'https://api.twilio.com/2010-04-01/Accounts/a99/Messages.json' -X POST \ --data-urlencode 'To=+86110110110110' \ --data-urlencode 'From=+1110110110' \ --data-urlencode 'Body=PC DOWN !' \ -u # 修改成你自己的 fi
端口存活的状态下就输出到日志,端口没存活的话就发短信
1 2 3 4 5 6 7 8 9 10 11 12 13 14
╭─root@sg-02 ~ ╰─# cat PC_STATUS PC NOW UP 2019-09-29 15:42:52 PC NOW UP 2019-09-29 15:50:01 PC NOW UP 2019-09-29 16:00:01 PC NOW UP 2019-09-29 16:10:01 PC NOW UP 2019-09-29 16:20:01 PC NOW UP 2019-09-29 16:30:01