PID
PID
Posted Oct 8, 2011 12:35 UTC (Sat) by nix (subscriber, #2304)In reply to: PID by Richard_J_Neill
Parent article: A Plumber's Wish List for Linux
You can't eliminate the race here, but you can narrow it radically by looking at the ppid:
#!/bin/bash
process2 &
PID=$!
# do stuff for several hours.
[[ "$(ps -o ppid= -p $PID)" == $BASHPID ]] && kill $PID
exit
Now this will only lose if $PID dies and a new process starts in that narrow window, or if this bash has many children and one of them started after $PID dies and has the same PID (which you can solve, if you really need to, by remembering PPID->PID mappings in an associative array).
