Description: My host for the Centos system, step-by-step instructions are written in the annotation inside, easy to read.
VI apachemonitor.sh #!/bin/bash url= "HTTP://127.0.0.1/" Curlit () {curl–connect-timeout 15–max-time 20–head–silent "$URL" |
grep ' 200′# 15 is the connection timeout, and if the HTTP service that accesses localhost exceeds 15s and still does not respond correctly to the 200-head code, it is judged as inaccessible. Doit () {if! Curlit then # if localhost Apache service does not return 200 headers correctly, an exception occurs. Execute the following command: Sleep top-n 1-b >>/var/log/apachemonitor.log # The top command content is written to date to file for reference/usr/bin/killall-9 apache2 &&am P /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 # is compatible, killing a variety of Apache processes. You can modify the sleep 2/etc/init.d/apache2 Start >/dev/null/etc/init.d/httpd start >/dev/null # Compatibility based on the features of your Apache service, and execute two AP
Ache Restart command, can be modified as needed. echo $ (date) "Apache restart" >>/var/log/apachemonitor.log # Write log Sleep 30 # wait 30 seconds after reboot completes, and then try again if! Curlit; Then # If you still cannot access, then: Echo $ (date) failed! Now Reboot computer! ">>/var/log/apachemonitor.log # writes to Apache that is still restarting the failed log Reboot # Reboot the machine. Actually restarting the entire server is a great way to do it. I do not propose.
Everyone according to the need to modify their own, such as text messages, e-mail alarm or something. Fi Sleep 180 fi 300 # 5 minutes after the script is run (prevents the server from restarting because Apache is not starting to cause a miscalculation) while true; Do # main circulation body doit >/dev/null sleep done then execute: chmod +x apachemonitor.sh Add boot entry: vi/etc/rc.d/rc.local my rc.local script
Content is: #!/bin/sh # This script would be executed *after* all the other init scripts.
# You can put your own initialization stuff in-if you don ' t # want to do the full Sys V style init stuff.
touch/var/lock/subsys/local/root/lampmonitor.sh