Check if PID exists in Bash
Solution 1
kill -s 0 $pid
will return success if $pid
is running, failure otherwise, without actually sending a signal to the process, so you can use that in your if
statement directly.
wait $pid
will wait on that process, replacing your whole loop.
Solution 2
It seems like you want
wait $pid
which will return when $pid
finishes.
Otherwise you can use
ps -p $pid
to check if the process is still alive (this is more effective than kill -0 $pid
because it will work even if you don't own the pid).
Solution 3
You might look for the presence of /proc/YOUR_PID
directory.
Solution 4
ps --pid $pid &>/dev/null
returns 0 if it exists, 1 otherwise
Solution 5
I always use the following
tail -f /dev/null --pid $PID
. It doesn't require explicit loop and isn't limited to your shell's children pids only.
Related videos on Youtube
JohnP
Updated on July 09, 2022Comments
-
JohnP almost 2 years
I want to stall the execution of my BASH script until a process is closed (I have the PID stored in a variable). I'm thinking
while [PID IS RUNNING]; do sleep 500 done
Most of the examples I have seen use /dev/null which seems to require root. Is there a way to do this without requiring root?
Thank you very much in advance!
-
atk about 13 yearsCheck out stackoverflow.com/questions/356100/…
-
SourceSeeker about 13 yearsPlease see Process Management. Can you show an example of using
/dev/null
which requires root? I can't imagine how that would be the case or how it would be applicable to this case. -
ndemou over 8 yearsWhat usage of /dev/null seems to require root? Can't think of any.
-
-
JohnP about 13 yearsThanks. Here is my current code:
while sleep 1; do kill -0 $PIDD || break; done
. -
Zouppen about 12 yearsThat's okay, if you are running the script in Linux only. To assure compatibility, it's better to use
kill -s
trick given above. -
sligocki almost 12 yearsNote:
kill -0 $pid
only works for processes you own.ps -p $pid
will work for any process. -
Harish over 11 yearsNote:
wait $pid
only works if you are the parent of$pid
. -
Bryan over 9 yearsIn my tests, I see that directory hang around for a short time after the process has died
-
ghoti almost 9 yearsThe
--pid
option forps
may work in your operating system, but it doesn't work in mine. Did the OP mention what OS he's running? If not, you should qualify answers you provide which are OS-specific and non-portable. -
ghoti almost 9 yearsThe
tail
command on FreeBSD and OSX does not include a--pid
option. If the OP isn't asking about a particular operating system, you should qualify any answers you provide which are platform-specific. -
Peter Eisentraut over 4 yearsNote:
wait
only works if$pid
is a child process of the shell. That is probably often the case in these scenarios, but not always. -
Dave over 2 years
wait
is not useful when scripting. If the goal is to quickly determine if a pid exists, then do something, wait would have be be bg which is inefficient. The second solution is better. Include removing the header and its usable:ps -p $pid | tail -n +2
-
sligocki over 2 yearsDave: The question literally states: "I want to stall the execution of my BASH script until a process is closed" which is exactly what
wait
does.wait
is perfectly usable in a script, I have used it many times! It simply depends what your use-case is.