分享一个shell写的网站监控脚本,监测网站的运行状态,此脚本分别检查网页状态和网页连接时间,在网站挂掉时及时发送报警邮件。
监控网站状态的shell脚本,代码:
复制代码代码示例:
#!/bin/sh
#edit:www.jbxue.com # weblist=/root/weblist.txt for list in `cat $weblist|grep -E -v "#|^$"` do httpcode=`curl -o /dev/null -s -w %{http_code} "$list"` httptime=`curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total: %{time_total}\n" "$list"|grep time_total|awk -F ":" '{print $2*1000}'` #if [ $httpcode = 200 ]||[ $httpcode = 301 ]||[ $httpcode = 302 ]||[ $httpcode = 403 ]||[ $httpcode = 401 ] if [ $httpcode = 200 ]||[ $httpcode = 301 ]||[ $httpcode = 302 ] then echo "$list is checked ok!" else echo "$list is down!" | mutt -s "web is down" admin@jbxue.com fi if [ $httptime -ge 10000 ] then echo "$list is timeout!" | mutt -s "web is timeout" admin@jbxue.com else echo "$list is connect ok!" fi done
第二步,创建要检查网站的列表,格式为http://xxx.xxx.xxx。
复制代码代码示例:
touch /root/weblist.txt
http://www.jbxue.com
添加执行权限:
添加计划任务:
复制代码代码示例:
crontab -e
*/3 * * * * /bin/sh /root/soft_shell/check-web.sh
运行结果图: |