分享一个shell写的网站监控脚本,监测网站的运行状态,此脚本分别检查网页状态和网页连接时间,在网站挂掉时及时发送报警邮件。
另外,大家还可以根据自己的需要进行扩展。
监控网站状态的shell脚本,代码:
vi check-web.sh
复制代码 代码示例:
#!/bin/sh
#edit:www.jb200.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|
linuxjishu/13830.html target=_blank class=infotextkey>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@jb200.com
fi
if [ $httptime -ge 10000 ]
then
echo "$list is timeout!" | mutt -s "web is timeout" admin@jb200.com
else
echo "$list is connect ok!"
fi
done
第二步,创建要检查网站的列表,格式为http://xxx.xxx.xxx。
复制代码 代码示例:
touch /root/weblist.txt
http://www.jb200.com
添加执行权限:
chmod +x /root/soft_shell/check-web.sh
添加计划任务:
复制代码 代码示例:
crontab -e
*/3 * * * * /bin/sh /root/soft_shell/check-web.sh
运行结果图:
