Michael O. :
I have a program running on a remote computer which shouldn't be stopped. I need to track when this program is stopped and immediately execute a command. PID is known. How can I do that?
Thomas :
You cannot wait
for non-child processes.
Probably the most efficient way in a shell would be to poll using the exit code of kill -0 <pid>
to check if the process still exists:
while kill -0 $PID 2>/dev/null; do sleep 1; done
This is both simpler and more efficient than any approaches involving ps
and grep
. However, it only works if your user has permission to send signals to that process.