Wait for a process to finish
To wait for any process to finish
Linux:
tail --pid=$pid -f /dev/null
Darwin (requires that $pid
has open files):
lsof -p $pid +r 1 &>/dev/null
With timeout (seconds)
Linux:
timeout $timeout tail --pid=$pid -f /dev/null
Darwin (requires that $pid
has open files):
lsof -p $pid +r 1m%s -t | grep -qm1 $(date -v+${timeout}S +%s 2>/dev/null || echo INF)
There's no builtin. Use kill -0
in a loop for a workable solution:
anywait(){ for pid in "$@"; do while kill -0 "$pid"; do sleep 0.5 done done}
Or as a simpler oneliner for easy one time usage:
while kill -0 PIDS 2> /dev/null; do sleep 1; done;
As noted by several commentators, if you want to wait for processes that you do not have the privilege to send signals to, you have find some other way to detect if the process is running to replace the kill -0 $pid
call. On Linux, test -d "/proc/$pid"
works, on other systems you might have to use pgrep
(if available) or something like ps | grep "^$pid "
.