A shell script to check on Apache

Jan 03, '05 09:12:00AM

Contributed by: Tomnibus

I thought this may be useful to anyone here who is running a web server. I use it on my linux boxes, and with my Apache crash last night, I am now implementing it on my Mac OS X machine.

Filename: checkapache

#!/bin/sh 

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

if [ "$run" ]; 
then 
  echo "Apache is running" 
else 
  apachectl start 
fi 
Save that file and make it executable to only root. (chmod 700, with root as the owner). Then edit your crontab by typing crontab -e, and enter this line:
/10 * * * * /path/to/file/checkapache 1> /var/log/apachecheck.log 2>/dev/null
I put the output to the log just as a record; it's overwritten every 10 minutes and errors go to /dev/null. You could remove that bit and allow your cron job to notify you if there is an error. Hope this helps someone; it keeps me from being woken up at 4am!

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050101130423871