Question

sh bothworkfile.txt >> cleanup_process.log

while [[ `ps -eaf | grep -c "cleanx"` -ge 2  ]]
do
sleep 10
echo "Please be patient, we are still cleaning stuffs ! "
done
    echo " Clean up is done."

Hi I am using ksh. The bothworkfile.txt contains few lines to run the process in the background.

cat bothworkfile.txt
cleanx data01.xsd*.* &
cleanx data01.xsd*.* &

Cleanx is an utility in my local shell. By default there is always a cleanx running in the background. So,

ps -eaf | grep -c "cleanx"

gives 1 always, but when a script calls it the number increases.

But now the issue is it seems i am not able to enter the while loop at all. And after running the script it prints " Clean up is done."

Please help me to understand why it is not entering while loop. Again, its ksh shell.

Was it helpful?

Solution

To make it easier to debug and see that the result is really what you are expecting, assign the result of ps -eaf | grep -c "cleanx" to a variable, then test the variable. I suspect it is evaluating to null or less than 2 right off the bat, which would cause the test to return false, thus skipping the loop. So, print it out before testing it so you know for sure. Something like this:

cleanx_count=$(ps -eaf | grep -c "cleanx")

printf "Cleanx_count: %d/n" ${cleanx_count}

while [[ ${cleanx_count} -ge 2  ]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top