Help with waiting while a process is running

bashscript

I'm looking for a bash script that checks if the Installer process is running and waits/pause if it then proceeds once the process is no longer running.

Here is what I have so far:

 #!/bin/bash
 PROCESS="Installer";
 PID=$(ps -A |grep -m1 "$PROCESS" | awk '{print $1}');
 while s=`ps -p $PID -o s=` && [[ "$s" && "$s" !='Z'  ]]; do
     echo "$PROCESS is Running, waiting for it to close"
     sleep 1
 done
 echo "$PROCESS is not Running";
 SCRIPT HERE

Best Answer

I took your script and changed

  • Changed the PID assignment to pgrep
  • Changed -o s= to -o stat= for ps
  • Removed the unnecessary ; at the end of some lines

It's a bit hard to test, but something like

#!/bin/bash
PROCESS="Installer"
PID=$(pgrep "$PROCESS")
while s=`ps -p $PID -o stat=` && [[ "$s" && "$s" != 'Z' ]]; do
    echo "$PROCESS is running, waiting for it to terminate"
    sleep 1
done
echo "$PROCESS is not running anymore"
SCRIPT HERE

should work.

If you are not worried about the Installer getting into Zombie state you could rely on the fact that ps exits with 1 if no process is found, and significantly simplify the loop condition.