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?
You should be simply doing:
The loop condition tests the exit status of the last command — in this case,
kill. The-0option (used in the question) doesn’t actually send any signal to the process, but it does check whether a signal could be sent — and it can’t be sent if the process no longer exists.(See the POSIX specification of the
kill()function and the POSIXkillutility.)The significance of ‘last’ is that you could write:
This too tests the exit status of
kill(andkillalone).