If I use a combination to kill a child process in batch and wait for it's termination, I use
kill $PID
wait $PID
If the process exists immediately, the wait
will fail, since the pid is not running anymore.
Is there a way to combine both statements to a single one to aviod the error?
Edit: The process I have to kill uses a tempfile; thus it has to be closed (and not just signaled to close) to start it again. Checking the return value of kill
does not help, since this indicates whether the signal was delivered successfully.
wait
? What do you really want to do? – Incessantkill
already returns a code that you can check to see if it was successful, right? ;-) If you want to kill the process no matter what, you can trykill -9 $PID
. – Electroballisticswhile [ $(kill ${PID}) ]; do "nothing" ;done
– Motoneuronkill $PID
returns immediately regardless of whether the job is actually killed or not. Just write a simply script that traps and ignoresSIGINT
, and see howkill
works (it will return0
, although the job is not killed). – UnconnectedcloseDone
make by the process)? Or make the process sleep for a while before exiting? Make the script sleep for a while after killing? Note that the latests are almost tricky. Do you have control over the source code of the process? – Incessantmktemp
.) – Unconnectedps
to check the status of the process after usingkill
? – Motoneuronwhile kill $PID 2>/dev/null; do sleep 0.01; done
. After PID exits, kill will fail & loop will break. There is a possibility of race condition though - another process may start in that 0.01 second, which can possibly have same PID... – Hirundine