Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A shell script to check on Apache OS X Server
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

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

if [ "$run" ]; 
  echo "Apache is running" 
  apachectl start 
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!
  • Currently 2.00 / 5
  You rated: 3 / 5 (10 votes cast)

A shell script to check on Apache | 9 comments | Create New Account
Click here to return to the 'A shell script to check on Apache' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A shell script to check on Apache
Authored by: TvE on Jan 03, '05 10:46:26AM

And why is Mac OS X's WatchDog not automagically restarting Apache in such a case - I mean that's what it suppose to do right…?

[ Reply to This | # ]
A shell script to check on Apache
Authored by: bluehz on Jan 03, '05 12:17:20PM

I am not sure that the non-server version of Mac OS X includes any sort of "watchdog" (aka daemon) tools for initiating Apache. I may be completely mistaken on this though.

I know on my Linux box that has mission critical Apache running - I use daemontools as a system wide daemon controller for processes (such as Apache) that do not have their own daemon mode. Daemontools will compile on Mac OS X just fine, although I don't have much experience using it in Mac OS X environment.

[ Reply to This | # ]

Authored by: Tomnibus on Jan 03, '05 02:14:52PM

hmmm, I'm fairly new to Mac OS X Server. I've been working on linux boxes for years now. I didn't know that there was a monitoring system on the server. I will/could probably use that then.

I do actually need to institute monitoring on my systems since they're taking over for other servers that are monitored by a third party (who won't be helping us at the new location.)

[ Reply to This | # ]
A shell script to check on Apache
Authored by: Tomnibus on Jan 08, '05 02:39:12AM

I don't know if you will get this comment but I have looked hi and low across the internet on how to get watchdog to check apache.

I put it in my script and I get this:
[code]2005-01-08 02:26:56 EST Started child "/usr/sbin/apachectl" as pid 18941.
2005-01-08 02:26:56 EST Reaped child process 18941 ("/usr/sbin/apachectl"); quit with exit status 0.
2005-01-08 02:26:56 EST Process "/usr/sbin/apachectl" respawning too rapidly![/code]

Why? Well, I looked online for that error and someone who put it into their inittab had the same problem and a response came:
Apache is not supposed to run from init. It runs as a daemon, ie.
it forks a child and the parent exits; init thinks it keeps dying
so it keeps trying to restart it.

So, essentially watchdog cannot watch apache.

[ Reply to This | # ]
A shell script to check on Apache
Authored by: hanksd on Jan 04, '05 12:42:26AM

My tech (a very sharp guy) and I have been struggling with a similar issue: FileMaker Unlimited running on an Xserve (10.3.7) crashes randomly when executing complex JDBC access routines via remote Java scripts. No ryme or reason to the number of events before it hangs FM. JDBC for FM 6 is extremely flaky especially with lots of "or" conditions on a lookup. (FM just released a new JDBC for FM v7... but real world still runs FM 6 which will not link easily to FM7 files so we cannot just upgrade the one module. Basically all support for our 16 FM Pro licenses, FM Server and FM Unlimited has been dropped), Problem is that the site MUST be up 24X7.

We need to detect when FM is hung, then kill it, then restart FM. What changes would you suggest to accomodate this sequence of events?

[ Reply to This | # ]
A shell script to check on Apache
Authored by: imacusr on Jan 04, '05 01:36:59AM
If you're using OS X Server on that Xserve, definitely check out watchdog; see
man watchdog
for more details.

[ Reply to This | # ]
A shell script to check on Apache
Authored by: TvE on Jan 05, '05 02:45:25AM

Yep, as mentioned in the first message in this thread ;-)

And no I cant find the wathdog on the client, so it IS a server thing (seems logical!)

[ Reply to This | # ]
A shell script to check on Apache
Authored by: da5idonimac on Jan 04, '05 08:01:54AM

does anybody have a similar script to check whether the built-in firewall is running?

my problem is: i have to turn off the firewall from time to time and i keep forgetting to turn it back on. i would use a shellscript with geeklog, but a menu display (or anything else) would also be helpful...

[ Reply to This | # ]
Similar Script
Authored by: Tomnibus on Jan 05, '05 12:31:43AM

Find out what the firewall filename is and use the same script, just replace 'grep httpd' with 'grep firewall-filename'

Should work fine.

[ Reply to This | # ]