Sleep while i > 0

Hi,
I have a script that runs a process at the beginning and I want to sleep/wait until this process is finished and then continue with the rest of the script. I am trying with this, but it is not working:

process=`ps -ef | grep "proc_p01 -c" | grep -v grep | wc -l`
if [ $process -gt 0 ]; do
  sleep 10
done

Actually, it never ends. Can you help me, please?

How do you start the process in the first place? Like proc_p01 -c & ?

If so, then just use wait in your script. It's meant for this task.

1 Like

how do you run the process ? is it a script called in your script ?

use wait

 
 
#!/bin/bash
 
/call/one/script.sh
wait
echo "End"

End will print after the script.sh is fully completed.

You call your thread Sleep while i > 0 and then use if [ $process -gt 0 ]; ?
Anyhow, if you run your process in the background using the & metachar, you can wait for it to finish and then continue the script. If you are using some other technique, place the ps | grep in the while test like

 while  ps|grep [9]029 ; do sleep 10; done 

Just a little example

sleep 20 &
p1=$!
echo "$p1 running"
sleep 15 &
p2=$!
echo "$p2 running"
sleep 10 &
p3=$!
echo "$p3 running"
sleep 5 &
p4=$!
echo "$p4 running"

date
wait $p1 $p2 $p3 $p4
date
echo "All sleep process are finished"

If that's intended to check for the existence of a process with pid 9029, it would be simpler to just use:

while ps -p 9029 >/dev/null; do
...
done

Regards,
Alister

Yes, I start it like that and 'wait' is perfect!
Thanks!

Are you doing some meaningful work between the time you start the process and the time you begin to wait for it? Or are you starting more than one process at a time asynchronously (backgrounded)?

If the answer to both questions is "no", then why even bother with & and wait ? Just run the process in the foreground. You won't need to explicitly wait or sleep on it.

Regards,
Alister