This doesn't work the way you seem to think it does:
while [[ $RTN -gt 0 ]]
echo "Before" $RTN
do
You want the echo to come after the do. With it before the do, it's part of the list-1 condition rather than the list-2 body. And, as per the bash docs (my bold):
The while command continuously executes the list list-2 as long as the last command in the list list-1 returns an exit status of zero.
You can see the difference between the following script, similar to yours:
#!/usr/bin/bash
RTN=2
while [[ $RTN -gt 0 ]]
echo "Before" $RTN
do
sleep 1
RTN=$(( RTN - 1 ))
echo "After" $RTN
done
which outputs (ad infinitum):
Before 2
After 1
Before 1
After 0
Before 0
After -1
Before -1
After -2
Before -2
When you move the echo to inside the body:
#!/usr/bin/bash
RTN=2
while [[ $RTN -gt 0 ]]
do
echo "Before" $RTN
sleep 1
RTN=$(( RTN - 1 ))
echo "After" $RTN
done
it then terminates properly:
Before 2
After 1
Before 1
After 0
<returns to prompt>
Once that change is made, the loop terminates correctly, given the values being generated by your ps command.
Additionally, if you want to find out what's in the process list (and probably causing an result of zero rather than negative one), output the process list before checking the count:
:
ps -C httpd # Add this line temporarily.
RTN=`ps -C httpd | wc -l`
:
gt 0. As I also stated, check the output of yourps, that will tell you why you're getting strange values.echoinside the loop body which basically made it totally different. I gather what you were trying to do on the subsequent edit was fix the starting value ofRETto match the actual output (10 rather than 5), and that's sensible, so I've incorporated that in the latest edit.pscommand.