Poor man’s process monitor

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.

4 comments

  1. Jon Ross

    I think that will work fine for now, but I would still want something that would monitor the site externally. Otherwise you could still have the problem of apache is running, but nobody can get to your site because of some load-balancer died, dns entry mixed up, hardware failure not noticed by the host. If those aren’t real worries then this is more than enough. The fact that it is a virtual box does mean that the company hosting it is probably monitoring it for hardware failures as you wouldn’t be the only one on a physical box, but there could still be other problems they aren’t looking out for.

    • Yeah, that would definitely be ideal.

      There are so many things that can go wrong with a website though, so I tend to focus on the things that have actually happened to me in the past. For example, my main dedicated server runs an IM bot that keeps an eye on my load average and checks to make sure all the critical processes are running. If something is wrong, it sends me an SMS message and an IM letting me know something is wrong. This has saved me several times in the past.

      I really want to make a website monitoring site for myself and also for others. Something more affordable than what is out there. I’ve done a bit of looking and most monitoring packages are stupid expensive, especially when you keep in mind that I have about 50 sites I’d need to monitor. Maybe I’ll make it as my website-of-the-month for July.

  2. Just off the top of my head…

    If you wanted something more frequent than every minute, you could create it as an init process and use a sleep command to pause for a smaller number of seconds and then just loop back through your script.

    Something like that anyway.

    • I think a minute is fine (it isn’t like Apache crashes all the time — I hope at least…), but doing it as an init process is definitely an interesting idea. That may be useful for some other projects I’m working on.

Leave a Reply

Your email address will not be published. Required fields are marked *