Poor man's process monitor

Jacob Allred
#linux

I lost some money a few days ago because Apache crashed on my VPS that is running the Fake Mail Generator. I’m too cheap to pay for web monitoring, so I had no idea it happened until I noticed my earnings were down.

So I had a few options:

  1. Pay for a web monitoring service. Knowing that Apache crashed would have been nice, but I still might have to get up in the middle of the night to start it back up.
  2. Roll my own web monitoring service. This is something I really want to do, and perhaps host on a couple of cheap VPS servers, but it still has the problem of not magically starting Apache for me if it dies.
  3. Install daemontools. This is probably the best option, as it would let me use supervise to make sure Apache restarts if it dies for some reason. But I’m lazy and just want a quick solution.
  4. Rig something together in 2 minutes that will get the job done.

I went with option #4, and this is what I came up with:

#!/bin/sh
string=`ps ax | grep -v grep | grep httpd`
 
if [ -z "$string" ]
then
/usr/local/apache2/bin/apachectl start
fi

I saved that to a file called keepApacheRunning and made it writable using chmod +x keepApacheRunning.

Basically this little script will see if httpd is in the process list, and if it isn’t, it will start Apache. Pretty straight forward.

To make it run, I added this entry to crontab:

* * * * * /www/keepApacheRunning

This will run it every minute. So my max downtime is about a minute. I can live with that.