简体   繁体   English

bash脚本杀死种族条件

[英]bash script kill race condition

I have a bash script which calls kill $PID , then waits for 2 seconds and if $PID still exists (by checking ps ), it calls kill -9 $PID as a fallback. 我有一个bash脚本,该脚本调用kill $PID ,然后等待2秒,如果$PID仍然存在(通过检查ps ),它将调用kill -9 $PID作为后备。 AFAIK, this is a very standard script to kill a process, found in many sample scripts on the net. AFAIK,这是杀死进程的非常标准的脚本,可以在网上的许多示例脚本中找到。

The script works fine (I am not worried about PID reuse here), but today I managed to catch a rare race condition where the process is still being killed in the 2 seconds, and the kill -9 hangs . 该脚本运行良好(我不担心PID在这里重复使用),但是今天我设法捕获了一种罕见的竞争条件,该过程在2秒内仍被杀死,而kill -9 hangs Here is the ps output: 这是ps输出:

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 is hanging because it has hit a defunct process (ie from the previous kill), ie PID 16635 is the process already killed by the first kill. kill -9挂起是因为它已经命中了一个已终止的进程(即从先前的kill中退出),即PID 16635是已经被第一个kill PID 16635的进程。

If I do a kill -9 on the kill -9 process (PID 17172) , then it is all good. 如果我做了kill -9kill -9 process (PID 17172)那么它是所有好的。

Is there anyway I can prevent the "kill -9" from hanging in the first place? 无论如何,我可以防止"kill -9"挂起吗? It is a fallback to ensure that the process is killed within 2 seconds. 这是确保该过程在2秒钟内被终止的备用方法。 I do not want to add another background check to check the fallback kill -9 . 我不想添加其他背景检查来检查后备kill -9

Update : 更新

The best answer I could find is to use the timeout command on the kill -9 : 我能找到的最佳答案是在kill -9上使用timeout命令:

timeout -s 9 2 kill -9 $PID

This will ensure that after 2 seconds, if the kill -9 hangs, the timeout will issue a kill -9 on it. 这样可以确保2秒钟后,如果kill -9挂起,超时将在其上发出kill -9

This may be a better way to check if the process is still alive, and if you should send the kill -9 这可能是检查进程是否仍在运行以及是否应该发送kill -9的更好方法。

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

This uses the sending of a signal through kill to make sure you have permission to send a kill signal to the process kill -0 . 这使用通过kill发送信号来确保您有权向进程kill -0发送kill信号。 Which may be better then checking just if the PID is still alive. 最好检查一下PID是否仍然有效。 Then if if you do have access to the program terminate the program. 然后,如果您确实有权访问该程序,则终止该程序。 The only other thing you can possible do is put in a longer sleep or something non trival like writing your own kill or checking the process state,etc. 您唯一可能做的另一件事就是让自己睡得更久或进行一些非琐事,例如编写自己的kill或检查进程状态等。

Also you can do another sleep and then check for the race condition by seeing after like 20 seconds if the kill -9 is still alive and then killing that process. 另外,您还可以进行另一次睡眠,然后在大约20秒后查看kill -9是否仍然存在,然后检查该进程,从而检查比赛条件。 Which would at least alleviate the stalling of the script but not solve the race condition. 这至少可以减轻脚本的停顿,但不能解决竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM