How To: Check if Apache is running & restart via cron job

I’ve personally been running into issues on my server lately that would for some still unknown reason crash Apache at some point during the night, and I’d have to find out when I first loaded up one of the sites on the server the next morning, then manually start Apache back up, and it had become quite tiresome. So I’ve done some investigating on how I can set up a shell script to check to see if Apache is currently active, and if not, restart the service.

I found a walk-through at maxosxhints.com, but had to make a few minor modifications to get it to work just right on my system.

First, I’ve had to create a shell script named apachetest on my system:

#!/bin/sh

run=`ps ax | grep /usr/sbin/httpd | grep -v grep | cut -c1-5 | paste -s -`

if [ “$run” ];
then
echo “Apache is running”
else
/etc/init.d/httpd start
fi

The sections in bold above are where I had to make modifications of the initial script to get it to work properly on my RHEL5 system. In the first section, the initial ps command was running simply this:

ps ax | grep httpd | grep -v grep | cut -c1-5 | paste -s -

However, after checking why it wasn’t restarting properly, I ran the command while the Apache web server was up, and got a list of about a dozen process ids, so that seems fine, but when I manually stopped the web server, and ran the same command, I was still getting 2 processes listed. After investigating, I had a couple of other processes that had the term “httpd” in the path, so the command above was pulling those, and considering that they were web server processes as well. So I pulled up the full process list, and determined that if I used the full path that was present in each of the true web server processes, I was able to run the check properly, so my final command looked like this:

ps ax | grep /usr/sbin/httpd | grep -v grep | cut -c1-5 | paste -s -

I also changed the last bold section to execute the proper httpd startup command on the system. It was unable to use the “service httpd start” command that I normally use on the command line and what is standard with Ensim systems, so I had to use the full path to the actual startup script which was /etc/init.d/httpd start.

Now, to set up the cron that we will use to run this script automagically, you can edit the crontab from the command line by executing the command “crontab -e“. I’m choosing to run this script once every single minute without fail. I’d like my web server to never be down for more than 60 seconds. This may not be the most processor friendly way to do this, however, the example I found was doing it every 10 minutes, but if my server is down for 10 minutes, I have lost money. And I don’t cherish the idea of losing money… do you?

To run the cron every minute like I am, you would add the following line to your crontab:

* * * * * /path/to/your/apachetest

You can find more information on setting up a cron job at wikipedia. Here’s a few easy samples I’ll go ahead include for common requests:

—- will run every 5 minutes
*/5 * * * * /path/to/your/apachetest

—- will run every 10 minutes
*/10 * * * * /path/to/your/apachetest

Hopefully this can help some others, as this is an issue I’ve ran across on several of my servers, and this is truly an easy solution for system admins to include a quick check that will restart Apache when it fails for whatever reason. This could very easily prevent those middle of the night phone calls from work screaming about websites being down. And who doesn’t enjoy their sleep!?

 

0 Responses to “How To: Check if Apache is running & restart via cron job”


  1. No Comments

Leave a Reply