轻松使用Shell脚本监控WEB服务是否正常
安装sendmail来发邮件
# yum -y install sendmail # /etc/init.d/sendmail start # chkconfig sendmail on
安装mutt邮件客户端,并设置相关信息
# yum -y install mutt # vim /etc/Muttrc set charset="utf-8" #设置发邮件编码 set envelope_from=yes set rfc2047_parameters=yes #解决附件乱码问题 set realname="报警" #发件人别名 set use_from=yes #指定是否显示别名 set [email protected] #发送人地址
脚本如下
#!/bin/bash Mail="[email protected]" FailCount=0 Retval=0 GetUrlStatus() { for ((i=1;i< =3;i++)) #使用i++判断访问次数,如果wget两次超时则判断网站异常 do wget -T 3 --tries=1 --spider http://${1} >/dev/null 2>&1 #-T超时时间,--tries尝试1次,--spider蜘蛛 [ $? -ne 0 ] && let FailCount+=1; #访问超时时,$?不等于0,则FailCount加1 done if [ $FailCount -gt 1 ];then Retval=1 Date=`date +%F" "%H:%M` echo -e "Date : $Date Problem : $url is not running." | mutt -s "URL Monitor" $Mail else Retval=0 fi return $Retval #如果返回值为0,就正常退出循环,不为0则继续循环 }for url in `cat url | sed '/^#/d'`do #GetUrlStatus $url && echo yes || echo no GetUrlStatus $urldonesleep 2m #死循环,设置没2分钟运行一次don
相关推荐
huha 2020-10-16
laisean 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
以梦为马不负韶华 2020-10-20
JohnYork 2020-10-16
Julyth 2020-10-16
applecarelte 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
wangzhaotongalex 2020-09-22
流年浅滩 2020-10-23
liujianhua 2020-10-22
woaimeinuo 2020-10-21
tufeiax 2020-09-03
laisean 2020-09-01
vvu 2020-09-16
libao 2020-09-16
Yyqingmofeige 2020-08-18
zhushixia 2020-08-17