分类: LINUX
2010-08-20 08:50:17
目的:
1.监控apache服务器的状态
2.当发现apache down机就自动重启apache服务
3.重启apache不成功,发邮件给管理员警告apache down机
#!/bin/sh
PORT=`netstat -na|grep "LISTEN"|grep "80"|awk -F[:" "]+ '{print $4}'`
APACHEIP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
while [ `whoami` == "root" ]
do
if [ "$PORT" == "80" ];then
echo "Apache is running......"
else
echo "restart apache"
/etc/init.d/httpd restart
PORT=`netstat -na|grep "LISTEN"|grep "80"|awk -F[:" "]+ '{print $4}'`
if [ "$PORT" == "80" ];then
echo "apache restart successful......"
else
echo "apache restart failure......"
echo "server: $APACHEIP apache is down,please try to restart apache!" > /var/log/apachemsg
mail -s "warn!server: $APACHEIP apache is down" < /var/log/apachemsg
fi
fi
break
done
赋予checkapache.sh可执行的权限
#chmod +x /usr/local/sbin/checkapache.sh
加入crontab,让系统五分钟检测一次apache状态
#crontab -l
*/5 * * * * /usr/local/sbin/checkapache.sh > /dev/null 2>&1
参考原文:http://blog.yahunet.com/?post=38