Operating System - HP-UX
1753809 Members
8422 Online
108805 Solutions
New Discussion юеВ

while loop count or found process

 
SOLVED
Go to solution
Ratzie
Super Advisor

while loop count or found process

I have a while look that currently checks for process running.
while [[ $MQSTAT -eq 0 ]]
do
sleep 30
mqstatus
echo $MQSTAT >> $SSLOG
done


What i want to do is prevent this to go endless if a process never stops.
How do I as a count.
So it will exit, if $MQSTAT -eq 0
or a count after 10 loops exits it.
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: while loop count or found process

You can add a count:
(( count = 10 ))
while (( count -= 1)); do
...
    if [ $MQSTAT -eq 0 ]; then
       break
    fi
    sleep 30
done

Ratzie
Super Advisor

Re: while loop count or found process

That is good, so what if I want to add a comment that i either ended because process was found or I ended because my loop hit 10...
I always get "waited too log exiting"

(( count=10 ))
while ((count -=1))
do
if [[ $MQSTAT -eq 1 ]]
then
break
fi
sleep 5
mqstatus
echo $MQSTAT >>test.log
done
echo "waited too log exiting" >>test.log

Re: while loop count or found process

well presumably MQSTAT will still be set, so you could do this messy approach:

(( count=10 ))
while ((count -=1))
do
if [[ $MQSTAT -eq 1 ]]
then
echo "MQSTAT = 1" >> test.log
break
fi
sleep 5
mqstatus
echo $MQSTAT >>test.log
done
[[ $MQSTAT -eq 1 ]] || echo "waited too log exiting" >>test.log

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Hein van den Heuvel
Honored Contributor
Solution

Re: while loop count or found process

Just test for 'count' after the loop.
If it is 0, then the loop count was exhausted, else the process was gone.

Hein.