tags:

views:

224

answers:

3

Hello guys this shell explain the issue , after executing the .sh file halt and nothing happen , any clue where is my mistake

its kill httpd if there is more than 10 sleep process and start the httpd with zero sleep process

#!/bin/bash

#this means loop forever
while [ 1 ];
do HTTP=`ps auwxf | grep httpd | grep -v grep | wc -l`;
#the above line counts the number of httpd processes found running
#and the following line says if there were less then 10 found running
if [ $[HTTP] -lt 10 ];
then killall -9 httpd;
#inside the if now, so there are less then 10, kill them all and wait 1 second
sleep 1;
#start apache
/etc/init.d/httpd  start;
fi;

#all done, sleep for ten seconds before we loop again
sleep 10;done
A: 

I can't see anything wrong with it.

This line:

if [ $[HTTP] -lt 10 ];

should probably be:

if [ ${HTTP} -lt 10 ];

even though yours works.

If you add this as the last line, you should never see its output since you're in an infinite while loop.

echo "At end"

If you do, then that's really weird.

Make your first line look like this and it will display the script line-by-line as it executes to help you see where it's going wrong:

#!/bin/bash -x
Dennis Williamson
the rueslt comes like this+ '[' 1 ']'++ ps auwxf++ grep httpd++ grep -v grep++ wc -l+ HTTP=72+ '[' 72 -lt 10 ']'+ sleep 10+ '[' 1 ']'++ ps auwxf++ grep httpd++ grep -v grep++ wc -l+ HTTP=70+ '[' 70 -lt 10 ']'+ sleep 10+ '[' 1 ']'++ ps auwxf++ grep httpd++ grep -v grep++ wc -l+ HTTP=67+ '[' 67 -lt 10 ']'+ sleep 10and never killed all httpd sleep process any clue
Because HTTP=72, for example and 72 is not `-lt` 10. You're testing for "less than" (`-lt` means "less than"), your comments say "less than" in two places, but your question says "kill httpd if there is more than 10". You have to change the condition you're testing for if you want it to work differently. Otherwise, based on the script and the output from the trace, it's working correctly.
Dennis Williamson
A: 

Why would you kill the child processes? If you do that you killing all ongoing sessions. Would it not be easier to setup your Webserver configuration so that it matches your needs?

As Dennis has mentioned already your script should look like:

#!/bin/bash

BINNAME=httpd   # Name of the process
TIMEOUT=10      # Seconds to wait until next loop
MAXPROC=10      # Maximum amount of procs for given daemon

while true
do
        # Count number of procs
        HTTP=`pgrep $BINNAME | wc -l`
        # Check if more then $MAXPROC are running
        if [ "$HTTP" -gt "$MAXPROC" ]
        then
                # Kill the procs
                killall-9 $BINNAME
                sleep 1
                # start http again
                /etc/init.d/httpd start
        fi
        sleep $TIMEOUT
done

Formating makes code more readable ;)

A: 

Watch out for killall if you are trying to write portable scripts. It doesn't mean the same thing on every system: while on linux it means "kill processes named like this" on some systems it means "kill every process I have permission to kill".

If you run the later version as root, one of the things you kill is init. Oops.

dmckee