注:此文摘自: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這一行便可。