I'd like to create a function that will wait until a job is completed before starting a new process. I'm aware of the wait
command that is built into bash, but it only works for child processes. It will fail if I start in a different bash session. I wrote a bash function using kill -0
that takes a pid and waits until the given process completes. However, it doesn't seem to work. Say the pid of a particular job sleep 60 &
is 4569, when I type ./pidwait.sh 4569
, nothing prints out. What am I doing wrong?
#!/usr/bin/env bash
function pidwait()
{
while kill -0 "$1"; do
echo "Process $1 still running..."
sleep 1
done
echo 'Process $1 is done'
}