Forestville NSW Australia tel 0410 532 923 email toivo@totaldata.biz
This tip shows how to monitor the status of the Apache web server and start it automatically if it has stopped.
RedHat 9, Apache, Sendmail
The purpose of the script is to check if the Apache web server, or the httpd service, is running. I noticed that the service can stop if the internet connection is down for a certain amount of time. Rather than bothering the end user responsible for the server, we can monitor the status of the service from the Linux system itself.
The solution requires the following components in /root:
The script is listed and explained below. The text files contain the messages to be sent to a list of recipients in a particular situation: webserverrestarted after the web server has been restarted, webservererror if the restart of the web server did not succeeed, and webserverok if you want to test the script running under cron a couple of times.
A new entry is inserted into /etc/crontab. This entry runs the script /root/checkwebserver every 10 minutes: the parameter 1,11,21,31,41,51 means one minute past the hour, 11 minutes past and so on. The asterisks simply mean every hour, day of the month, every month and every day of the week.
02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly 1,11,21,31,41,51 * * * * root /root/checkwebserver
#!/bin/bash # check if httpd is running, if not start it # send email messages after restart PSFILE=/root/pshttpd.txt; PSFILE2=/root/pshttpd2.txt; ps -A | grep httpd > $PSFILE if ! test -s $PSFILE; then service httpd start; sleep 20; ps -A | grep httpd > $PSFILE2 if test -s $PSFILE2; then mail mobuser@totaldata.biz -c officemanager@totaldata.biz,toivo@totaldata.biz -s "Web server Restarted" < /root/webserverrestarted; else mail mobuser@totaldata.biz -c officemanager@totaldata.biz,toivo@totaldata.biz -s "Web server Stopped" < /root/webservererror; fi fi
Make sure the script has the execution rights:
cd /root chmod 755 checkwebserver
The script starts by setting two variables, PSFILE and PSFILE2, to be used as names of two working files. The ps command lists the all the running processes or services. The output from ps is piped through to the grep command, which filters out all the lines without the text httpd - the name of the service running the Apache web server. The lines containing httpd are redirected to PSFILE.
The if statement checks if the length of the text file is zero, in other words, if no lines containing httpd were found, which indicates that the httpd service is not running. If this is the case, the service is started and the script waits for 20 seconds. It then checks again if httpd is among the running processes. If the service has started, an email message is sent to and copied to a list of people. If the service has not started, an error message is sent to the same list of people
.Modify the script to send you a message every time the script is run:
... ps -A | grep httpd > $PSFILE if test -s $PSFILE; then mail mobuser@totaldata.biz -s "Web server running OK" else service httpd start; sleep 20; ...and so on
The contents of the following text files is redirected to the mail command and becomes the body of the email message. A message is sent every time an exception happens: if the web server had to be restarted, and also if the web server had to be restarted and the restart attempt failed.
The web server has been restarted. Every 15 minutes the status of the web server is checked automatically. The automatic status check restarted the web server. This message has been sent to the Office Manager, IT Manager and IT Support.
The web servermail has stopped. Every 15 minutes the status of the web server is checked automatically. The automatic status check tried to restart the web server but failed. This message has been sent to the Office Manager, IT Manager and IT Support.
Webmail is running OK. This is a test message only.