shell脚本监控网站状态
#!/bin/sh date=`date +"%Y%m%d-%H%M"`
title="status"
contentFail="status is not ok:"
contentSuccess="status is ok:"
url="https://www.abc.com"
status=`curl -m -s -I $url | grep HTTP | awk '{print $2}'` #echo "status: $status" cd /data/shell
laststatus=`cat status.log` if [ "$status" == "" ]
then
if [ "$laststatus" != ]
then
/usr/bin/python /shell/mail.py "$title" "$contentSuccess $url $date $status"
echo "" > status.log
fi
else
if [ "$laststatus" == ]
then
/usr/bin/python /shell/mail.py "$title" "$contentFail $url $date $status"
echo "$status" > status.log
fi
fi
mail.py
from email.header import Header
from email.mime.text import MIMEText
import smtplib
import sys def sendmail(subject, content):
sender = 'abc@163.com'
password = 'abc'
recipients = 'abc@qq.com'
host = 'smtp.abc.com'
msg = MIMEText(content, 'plain', 'utf-8')
msg['From'] = sender
msg['To'] = recipients
msg['Subject'] = Header(subject, 'utf-8').encode()
server = smtplib.SMTP_SSL(host, )
server.login(sender, password)
server.sendmail(sender, [recipients], msg.as_string())
server.quit() sendmail(sys.argv[],sys.argv[])
设置定时任务
crontab -e
*/ * * * * /shell/status.sh