终止一系列进程

3
当我输入命令 ps -ef |grep sharatds 时,我会得到一个进程列表。
sharatds 13164 13163  0 20:53 pts/2    00:00:00 [bt.C.256] <defunct>
sharatds 13165 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13199 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13233 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13267 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13301 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13335 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13369 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13403 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13437 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13471 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13505 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13539 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13573 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13607 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13641 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13675 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13709 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13743 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13777 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13811 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13845 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13879 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>
sharatds 13913 13163  0 20:53 pts/2    00:00:00 [rsh] <defunct>

我想杀死所有最后一列为“defunct”的进程。
有没有人可以帮我写一个脚本?

3
如果可以的话,最好避免使用ps命令进行grep操作。如果有pgrep -upkill -u命令,则使用它们,否则使用ps -u。使用grep命令会存在误报的风险。Brandon是正确的,它们已经死了。 - Dennis Williamson
2个回答

7
这样做就可以了:
ps -ef | grep sharatds | awk '{print $2}' | xargs kill

4

我通常会这样做:

kill $(ps -ef |grep sharatds|awk '{print $2}')

编辑:等等!那些是已经死亡的进程,无法被进一步杀死!父进程需要运行wait()来读取它们的状态,以便将它们清理并从进程表中删除。


1
我真的很喜欢这种方法,很好注意到你也可以执行 sudo kill -9 $(ps -ef | grep sharatds | awk '{print $2}') 如果你想在常规kill无法工作的情况下进行“超级杀死”。(不适合胆小者)LOL - Mike Kormendy
或者杀死父进程。当父进程死亡时,子进程被init(进程1)继承,它会自动回收僵尸子进程。 - anthony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接