1833877 Members
1548 Online
110063 Solutions
New Discussion

Re: While Loop Not Exit

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

While Loop Not Exit

I tested a wonderful script from sb. of this forum in an attempt to apply it to my situation. But I can not have the while loop end unless a hard keystroke of Ctrl+C is enforced. Syntac looks all right.

Here are the lines of script:

***************************************
#!/bin/sh
NOTIFIED=0
COMMAND=DEVP #test DEVP db up status
MAILBOX=zzzz@wwwyyy.com
CHECKINTERVAL=10 #launch every 10 seconds
while true
do
PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`
if test ${PROCS} -gt 1 # cannot use "-eq 1"
then
if test ${NOTIFIED} -eq 0
then
echo "Service ${COMMAND} is up again!!\n `date`" | mailx -s "Service Start
ed" ${MAILBOX}
NOTIFIED=1
fi
else
echo "Servie ${COMMAND} is down!!\n `date`" | mailx -s "Service Stopped" ${M
AILBOX}
NOTIFIED=0 # need comment out to avoid unlimited checking/mailxing
fi
sleep ${CHECKINTERVAL}
done
******************************************

All comments are appreciated!

Steven

Steve
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: While Loop Not Exit

You have a "while true" loop without a break or an exit statement. What do you expect it to do except loop forever? It would help if your listed the conditions upon which you want it to exit.
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: While Loop Not Exit

If I'm reading your script correctly, you want to
check, once every 10 seconds, if a command is
running and send a mail with the status.

How long you want this to run? In the current form,
this script would run forever. Now, if that's what
you want, you could run the script in the background
and it would keep doing the check you want. If
you want it to run for few hours, or want it to exit
under certain conditions, then you will have to insert
some code that checks this condition at the beginning
(or before end) of the "while" loop and exit.

- Biswajit
:-)
Bill Hassell
Honored Contributor

Re: While Loop Not Exit

The script is designed to run forever. It keeps on monitoring the process in $COMMAND and sends mail when it stops, and continues monitoring. It even has a flag to send only one notice when the process stops. A script like this would be run in the background. A couple of notes:

PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`

has a basic problem. ps and grep are seldom reliable. While $COMMAND is set to "DEVP", the above statement can return several matches because grep never looks at the process name...it looks at the entire line. The grep will match DDEVP, /opt/DEVPDIR/prog and many other strings, whether it is a program name or a parameter. ps is so much more powerful than most admins know. Using ps -ef is a big load on a busy server so let's have ps do the searching in the process table. Change ps to do the searches with 100% accuracy:

COMMAND=DEVP
UNIX95= ps -C $COMMAND

Now ps always returns EXACTLY the number of processes that have a basename of DEVP. The UNIX95= construct is needed to enable 3 useful options: -C -H -o. ps always supplies a header as line #1, so we have to remember that wc -l is 1 higher than needed.

Throughout the script, the construct $(...) is used for "Command Substitution" except in the PROCS= statement. $(...) is always preferred, so the code would work better like this:

COMMAND=DEVP
...
let PROCS=$(UNIX95= ps -C $COMMAND | wc -l)-1
if test $PROCS -gt 0
then
...process is up
else
...process is down
fi

And with all scripts, always test the script by running it in trace mode:

sh -x ./myscript

Now you'll see all the steps and the results.


Bill Hassell, sysadmin