注:此文摘自:http://www.111cn.net/sys/linux/63718.htmlinux
原理:经过服务器本地访问自身Apache服务(与用户访问网站相似),如超过15s没有返回正常的220头代码信息,说明Apache服务已经中止运行了,则当即重启httpd服务。bash
一、在Linux服务器上执行vi编辑一个新脚本,并把下面脚本代码复制进去,而后退出并保存服务器
[root@localhost /]# vi /opt/autorshttpd
#!/bin/bash
URL="http://127.0.0.1/"
curlit()
{
curl --connect-timeout 15 --max-time 20 --head --silent "$URL" | grep '200'
}
doit()
{
if ! curlit; then
/etc/init.d/httpd restart > /dev/null
fi
}
while true; do
doit > /dev/null
sleep 10
donecurl
二、给脚本赋予可执行权限网站
[root@localhost /]# chmod 755 /opt/autorshttpdurl
三、执行脚本.net
[root@localhost /]# sh /opt/autorshttpd &rest
注:在这里sh命令后面要加个&符号,是为了方便咱们远程SSH操做的,若是不加&符号,那关闭SSH远程界面,此进程也就随之结束了,加上&符号,即便关闭SSH远程也能够让程序在后台运行,别忘了用exit命令退出登录后,再关闭SSH远程界面htm
四、让脚本开机自动运行进程
[root@localhost /]# vi /etc/rc.local
在最后面加上sh /opt/autorshttpd这一行便可。