Take the 2-minute tour ×

It's just what the question asks. Also, all I have is the PID, and the shell I am running the command from is not necessarily the shell that initially invoked the process. Any ideas?

share|improve this question
I know it isn't kosher to ask a question about the question, but... how do you know the PID of a process that hasn't been created? PIDs can be rather random. Sure, in practice they are assigned linearly but you don't know how many processes will start between time A and B or if the PID counter will wrap. –  Thomas M. DuBuisson Nov 10 '10 at 0:01
@TomMD maybe he uses pidof to find the PID of the process at runtime, or maybe something else. There are a hundred different ways he could have the PID of a process, while not hard-coding it in the shell script ;) –  MartinodF Nov 10 '10 at 0:27
@TomMD I know the PID because I know the process name, and I can use the ps command to get the PID. –  B Johnson Nov 10 '10 at 11:59
@TomMD: I don't think there's any problem with asking questions about questions. –  Dennis Williamson Nov 10 '10 at 15:32
@all - oops. Somehow I completely misread the question as "how to block until a process exists" and under that question you can probably better understand my confusion :-). –  Thomas M. DuBuisson Nov 10 '10 at 17:34
add comment

3 Answers

up vote 2 down vote accepted
while ps -p $PID >/dev/null 2>&1; do sleep 1; done


while ps -p $PID >/dev/null 2>&1; do :; done
share|improve this answer
That's the best answer that I've seen. I wonder why bash's built-in version of wait only works when it is a child process of the current shell. –  B Johnson Nov 10 '10 at 12:39
@B Johnson: Because it takes a job ID which is only valid in the current shell instead of taking a PID. There are probably other reasons internal to the way job control works. –  Dennis Williamson Nov 10 '10 at 15:32
add comment

It's not a very neat way to do it, but you may continuously issue kill(2) system calls to the specified pid, putting zero as a signal. The first time kill doesn't succeed by setting errno to ESRCH, you may conclude that the process has exited.

share|improve this answer
Such as ​0​​. –  Ignacio Vazquez-Abrams Nov 9 '10 at 21:55
kill -0 ${PID} is the preferred way. It is portable and fast since it uses just one simple syscall. –  Metiu Nov 10 '10 at 17:15
add comment

You could check if the dir /proc/PID exists. It seems cleaner than sending multiple signals

share|improve this answer
add comment

Your Answer


