用shell和php脚本实现定时检测服务器情况
实现原理,利用 cron来定时执行一个shell脚本,如果发现服务器不通或者上次故障后恢复正常,则发送邮件.否则不用发送邮件. 目前此Shell在FreeBSD和Linux下均可以正常运行.
ping.sh
#!/bin/bash
if [ $# -ne 1 ]
then
echo 'must have one param,must be ip address format!'
exit
fi
ip=$1
tmpfile=/tmp/$ip.txt
if [ -f $tmpfile ]; then
lastmsg=`cat $tmpfile`
else
lastmsg='YES'
fi
ret=`ping -c 3 $ip | grep ttl | wc -l`
if [ $ret -lt 2 ]; then
echo 'NO' > $tmpfile
/usr/local/bin/php /data/haohtml.com/www/ping.php $ip > /dev/null 2>&1
elif [ $lastmsg = 'NO' ]; then
echo 'YES' > $tmpfile
/usr/local/bin/php /data/haohtml.com/www/ping.php $ip 1 > /dev/null 2>&1
fi
ping.php
By admin
read more