I wrote a bash script to restart Apache when it hanged and send email to the admin. The code is shown below. the code will restart Apache if the number of Apache process is zero. The problem is: Apache some time hangs and processes is still not zero,so in this case the script will not restart Apache.
The needed is: how do I modify the code to restart Apache if it hanged and the processes is not zero.

Fix the disease, not the symptoms. You should be asking how to determine why apache is hanging (probably in ServerFault) and then fix that... not how to restart it when it hangs.
– JulianoJan 30 '10 at 16:30

Why did you reinvent the wheel here, the startup scripts found in /etc/rc.d or similar (depending on unix/linux installation) has the capability to do a restart, but then again, check your error logs as to why you have to restart the server, usually /etc/rc.d/3/rc.httpd restart or similar...
– t0mm13bJan 30 '10 at 16:40

1

Hi. Juliano your are right. I am looking for the root cause of problem but I think this kind of scripts is a "must have" because it is possible in future it will hang and this script will temporary solve the problem.
– usef_ksaJan 30 '10 at 17:16

hi, Tommieb75. I will not restart the server, I will only restart the service "Apache".
– usef_ksaJan 30 '10 at 17:17

"Server" refers to Apache in this case, not the computer.
– Dennis WilliamsonJan 30 '10 at 19:10

4 Answers
4

We used to have Apache segfaulting sometimes on a machine; here's the script we used trying to debug the problem while keeping Apache up. It ran from cron (as root) once every minute or so. It should be self-explanatory.

Which one is better to test the apache server, using wget or check the status of the apache using /etc/init.d/httpd status? I am thinking using wget will put more stress to the server.
– geckobDec 18 '15 at 11:43

2

I would certainly recommend using an actual http request, because if Apache just hangs deadlocks or something (i.e. the process is still running), the init script won't tell you. And if your server cannot handle one request per minute, a crashing Apache is the least of your worries ;)
– ThomasDec 19 '15 at 12:19

Thanks. But I think that will work for checking mysql server status?
– geckobDec 21 '15 at 2:03

2

@jessica You'd have to rm $THEDIR/mail $THEDIR/robots.txt before you do rmdir $THEDIR. Also consider passing -O $THEDIR/robots.txt instead of -P $THEDIR to just overwrite the output file if an earlier run of the script didn't clean up for some reason.
– ThomasApr 13 '16 at 6:41

There are a number of scripts and tools out there to 'daemonize' apps and watch over them. As you seem to be on Debian or Ubuntu, have a look at the packages daemon and daemontools. I am sure there are others too.