Linux下一个智能重启Apache服务器的脚本分享
说明下:我的主机为Centos系统,各步操作的说明都写在了注释里面,方便阅读。
viapachemonitor.sh
#!/bin/bash
URL=”http://127.0.0.1/”
curlit()
{
curl–connect-timeout15–max-time20–head–silent“$URL”|grep'200′
#上面的15是连接超时时间,若访问localhost的HTTP服务超过15s仍然没有正确响应200头代码,则判断为无法访问。
}
doit()
{
if!curlit;then
#如果localhost的apache服务没有正常返回200头,即出现异常。执行下述命令:
sleep20
top-n1-b>>/var/log/apachemonitor.log
#上面将top命令内容写入日至文件备查
/usr/bin/killall-9apache2&&/usr/bin/killall-9php5-cgi&&/usr/bin/killall-9httpd
&&/usr/bin/killall-9http&&/usr/bin/killall-9apache
&&/usr/bin/killall-9php-cgi>/dev/null
#兼容起见,杀死了各种apache的进程。可以根据自己apache服务的特点修改
sleep2
/etc/init.d/apache2start>/dev/null
/etc/init.d/httpdstart>/dev/null
#兼容起见,执行了两种apache重启命令,可根据需要自己修改。
echo$(date)“ApacheRestart”>>/var/log/apachemonitor.log
#写入日志
sleep30
#重启完成后等待三十秒,然后再次尝试一次
if!curlit;then
#如果仍然无法访问,则:
echo$(date)“Failed!NowRebootComputer!”>>/var/log/apachemonitor.log
#写入apache依然重启失效的日志
reboot
#重启机器呗。实际上重启整个服务器是一种非常不得已的做法。本人并不建议。大家根据需要自己修改,比如短信、邮件报警什么的。
fi
sleep180
fi
}
sleep300
#运行脚本后5分钟后才开始正式工作(防止重启服务器后由于apache还没开始启动造
成误判)
whiletrue;do
#主循环体
doit>/dev/null
sleep10
done
然后执行:chmod+xapachemonitor.sh
添加开机启动项:
vi/etc/rc.d/rc.local
我的rc.local脚本内容为:
#!/bin/sh
#
#Thisscriptwillbeexecuted*after*alltheotherinitscripts.
#Youcanputyourowninitializationstuffinhereifyoudon't
#wanttodothefullSysVstyleinitstuff.
touch/var/lock/subsys/local
/root/lampmonitor.sh