Linux next smart restart Apache server script sharing
- 2020-05-14 05:23:11
- OfStack
My host is the Centos system. The instructions for each step are written in the notes for easy reading.
vi apachemonitor.sh
#!/bin/bash
URL= " http://127.0.0.1/ "
curlit()
{
curl � connect-timeout 15 � max-time 20 � head � silent " $URL " | grep '200 '
# The above 15 Is the connection timeout if accessed localhost the HTTP Service more than 15s Still no correct response 200 The header code is judged to be inaccessible.
}
doit()
{
if ! curlit; then
# if localhost the apache The service did not return normally 200 The head, there is an abnormality. Execute the following commands:
sleep 20
top -n 1 -b >> /var/log/apachemonitor.log
# The above will be top Write the contents of the command to a solstice file for reference
/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
# Compatibility kills all sorts apache In the process. You can do it on your own apache Service feature modification
sleep 2
/etc/init.d/apache2 start > /dev/null
/etc/init.d/httpd start > /dev/null
# For compatibility, two are implemented apache Restart the command and modify it as needed.
echo $(date) " Apache Restart " >> /var/log/apachemonitor.log
# Written to the log
sleep 30
# Wait until the restart is complete 310 Seconds, and then try again 1 time
if ! curlit; then
# If it is still not accessible:
echo $(date) " Failed! Now Reboot Computer! " >> /var/log/apachemonitor.log
# write apache Restart the failed log
reboot
# Let's restart the machine. Actually restarting the entire server is 1 A very last resort. I don't recommend it. Everyone according to the need to modify their own, such as SMS, email alarm and so on.
fi
sleep 180
fi
}
sleep 300
# After running the script 5 Minutes after the start of formal work (to prevent server restarts due to apache We haven't started building it yet
A miscalculation)
while true; do
# The main loop body
doit > /dev/null
sleep 10
done
Then execute: chmod +x apachemonitor.sh
Add startup items:
vi /etc/rc.d/rc.local
my rc.local The script content is:
#!/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