說明下:我的主機(jī)為 Centos 系統(tǒng),各步操作的說明都寫在了注釋里面,方便閱讀。
vi apachemonitor.sh#!/bin/bashURL=”http://127.0.0.1/”curlit(){curl –connect-timeout 15 –max-time 20 –head –silent “$URL” | grep '200′# 上面的15是連接超時(shí)時(shí)間,若訪問localhost的HTTP服務(wù)超過15s仍然沒有正確響應(yīng)200頭代碼,則判斷為無法訪問。}doit(){if ! curlit; then# 如果localhost的apache服務(wù)沒有正常返回200頭,即出現(xiàn)異常。執(zhí)行下述命令:sleep 20top -n 1 -b >> /var/log/apachemonitor.log# 上面將top命令內(nèi)容寫入日至文件備查/usr/bin/killall -9 apache2 && /usr/bin/killall -9 php5-cgi && /usr/bin/killall -9 httpd&& /usr/bin/killall -9 http && /usr/bin/killall -9 apache&& /usr/bin/killall -9 php-cgi > /dev/null# 兼容起見,殺死了各種apache的進(jìn)程。可以根據(jù)自己apache服務(wù)的特點(diǎn)修改sleep 2/etc/init.d/apache2 start > /dev/null/etc/init.d/httpd start > /dev/null# 兼容起見,執(zhí)行了兩種apache重啟命令,可根據(jù)需要自己修改。echo $(date) “Apache Restart” >> /var/log/apachemonitor.log# 寫入日志sleep 30# 重啟完成后等待三十秒,然后再次嘗試一次if ! curlit; then# 如果仍然無法訪問,則:echo $(date) “Failed! Now Reboot Computer!” >> /var/log/apachemonitor.log# 寫入apache依然重啟失效的日志reboot# 重啟機(jī)器唄。實(shí)際上重啟整個(gè)服務(wù)器是一種非常不得已的做法。本人并不建議。大家根據(jù)需要自己修改,比如短信、郵件報(bào)警什么的。fisleep 180fi}sleep 300# 運(yùn)行腳本后5分鐘后才開始正式工作(防止重啟服務(wù)器后由于apache還沒開始啟動(dòng)造成誤判)while true; do# 主循環(huán)體doit > /dev/nullsleep 10done然后執(zhí)行: chmod +x apachemonitor.sh添加開機(jī)啟動(dòng)項(xiàng):vi /etc/rc.d/rc.local我的rc.local腳本內(nèi)容為:#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch /var/lock/subsys/local/root/lampmonitor.sh
新聞熱點(diǎn)
疑難解答
圖片精選