linux - bash script kill race condition -


i have bash script calls kill $pid, waits 2 seconds , if $pid still exists (by checking ps), calls kill -9 $pid fallback. afaik, standard script kill process, found in many sample scripts on net.

the script works fine (i not worried pid reuse here), today managed catch rare race condition process still being killed in 2 seconds, , kill -9 hangs. here ps output:

root     17172  0.0  0.0   7920  1668 ?        s    jul16   0:00 /bin/kill -9 16635 root     17173  0.0  0.0      0     0 ?        z    jul16   0:00 [kill] <defunct> 

the kill -9 hanging because has hit defunct process (ie previous kill), ie pid 16635 process killed first kill.

if kill -9 on kill -9 process (pid 17172), good.

is there anyway can prevent "kill -9" hanging in first place? fallback ensure process killed within 2 seconds. not want add background check check fallback kill -9.

update:

the best answer find use timeout command on kill -9:

timeout -s 9 2 kill -9 $pid

this ensure after 2 seconds, if kill -9 hangs, timeout issue kill -9 on it.

this may better way check if process still alive, , if should send kill -9

kill [pid] sleep 5 kill -0 [pid] if [ $? -eq  0 ] ;   echo 'process not terminated'   kill -9 [pid] else   echo 'process terminated' fi 

this uses sending of signal through kill make sure have permission send kill signal process kill -0. may better checking if pid still alive. if if have access program terminate program. other thing can possible put in longer sleep or non trival writing own kill or checking process state,etc.

also can sleep , check race condition seeing after 20 seconds if kill -9 still alive , killing process. @ least alleviate stalling of script not solve race condition.


Comments