need help: shell script to restart apache when no. of processes keeps growing

I need a shell script to kill apache and restart it, in case the number of processes keeps growing. The logic is like the below, but I don't know how to get the number and neither the syntax. Could somebody kindly help?

if no_of_processes (ps ax � grep httpd) > 200
then
killall httpd
apachectl start

I'm using Fedora Core3

if [ `ps -fu $USERNAME  | awk '/processname/ { x++ } END{print x}'` > 200 ]
then
 echo "exceeded"
 #do killall and restarting
fi

[[ $(ps -ax | grep httpd| wc -l) -gt 200 ]]&& { kill -9 $(ps -ax | grep httpd | awk '{ print $1; }'); apachectl start; }

http://httpd.apache.org/docs/2.0/mod/mpm_common.html\#maxclients

Cheers
ZB

kill -9???????????????????????????

killall httpd -------------> I assumed killing all the httpd processes...

Thank you all so much for the help. With your help I think I can write my first shell script, which I hope will avoid the issue.

The real issue is when that happens, there are accordingly growing number of records like those below in the httpd log file.

[Mon Mar 05 16:08:15 2007] [warn] child process 24829 still did not exit, sending a SIGTERM
and
[Mon Mar 05 16:08:21 2007] [error] child process 24829 still did not exit, sending a SIGKILL

I have no clue why the number of processes keep growing and can't be killed automatically by the system.

I think it has something to do with mysql, because at the early stage before the server is brought down, dynamic pages using mysql slow down while static pages of plain text still respond normally.

At the moment I simply don't know what I can do to get closer to finding out what is the origin of the issue. Can anybody give me some advices or suggestions or any comments?

killall ?????????????????????????

That's the first thing that comes to your mind?

Ever considered trying to stop something in a decent way instead of using kill as first option?

Thanks sb008, do you any suggestion?
The problem is that when the number of processes grows abnormally, finally not only apache but also the whole server can be brought down.
By now I haven't found out the cause, but just thought that at lease I can save the server from being down by using kill or killall. I really have no better idea at the moment.

Have you tried setting MaxClients within your Apache configuration as I indicated above?

You should really be concentrating your efforts on trying to find the cause of the issue, rather than circumventing it with a shonky workaround.

Cheers
ZB

sb008,

I know that kill command is not the best bet to stop a process...but,I have observed in many such kind of cases that kill 'is a better choice' as stopping the process using the 'decent way' might end up in many 'zombie' processes...

Why is that? :slight_smile:

Then possibly that should be a bug with respect to the parent which refuses to reap the exit status and process statistics of the child.

Thanks ZB, you are absolutely right.
I tried to change MaxClients, but because the number of processes grows almost infinitely, I know something must be wrong which is causing the issue.
Sadly I only know it should has something to do with mysql, but no idea how to get closer.

Or never receives the dead of child signal.

This is most likely to obtain for orocess which are started, to put it simple, with the nohup option. When the initional "httpd" daemon dies, parenthood for all child "httpd" daemons is passed over to the "init" process.

This is bug sensitive.

Therefore a kill, as shown, will more likely cause zombie processes since the order in which the "httpd" daemons are killed is random meaning the parent "httpd" is almost for sure killed before at least some of the "httpd" child processes.

Furthermore a "kill -9" might result in a not proper cleaning of the resources which are used by the process. When killing enough processes like that, at some stage the system will end up with an internal chaos which can cause all kind of unexpected/unwanted behavior like zombie processes starting to occur.

The "kill -9" is meant for when all other options fail, and surely not as first option.

I used 'killall httpd' to kill all 'httpd' processes and start apache again. So far it's working well. I know little about killall command except for it killing all the processes.