Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using while or until to wait until a PID doesn't exist

Tags:

bash

process

I have been using Bash to wait until a PID no longer exists. I've tried

#!/bin/bash
while [ kill -0 PID > /dev/null 2>&1 ]; do
    //code to kill process
done
//code to execute after process is dead

as well as

#!/bin/bash
until [ ! kill -0 PID > /dev/null 2>&1 ]; do
    //code to kill process
done
//code to execute after process is dead

Both these examples either fail to work, or keep on looping after the process has ended. What am I doing incorrectly?

like image 413
hexacyanide Avatar asked Jul 11 '12 05:07

hexacyanide


1 Answers

Also you can do in unixes with procfs (almost all except mac os)

while test -d /proc/$PID; do
     kill -$SIGNAL $PID
     # optionally
     sleep 0.2
done
like image 162
Felipe Buccioni Avatar answered Oct 19 '22 12:10

Felipe Buccioni