简体   繁体   English

如何杀死僵尸进程

[英]How to kill zombie process

I launched my program in the foreground (a daemon program), and then I killed it with kill -9 , but I get a zombie remaining and I m not able to kill it with kill -9 .我在前台启动了我的程序(一个守护程序),然后我用kill -9杀死了它,但我得到了一个僵尸,我无法用kill -9杀死它。 How to kill a zombie process?如何杀死僵尸进程?

If the zombie is a dead process (already killed), how I remove it from the output of ps aux ?如果僵尸是一个死进程(已经被杀死),我如何从ps aux的 output 中删除它?

root@OpenWrt:~# anyprogramd &
root@OpenWrt:~# ps aux | grep anyprogram
 1163 root      2552 S    anyprogramd
 1167 root      2552 S    anyprogramd
 1169 root      2552 S    anyprogramd
 1170 root      2552 S    anyprogramd
10101 root       944 S    grep anyprogram
root@OpenWrt:~# pidof anyprogramd
1170 1169 1167 1163
root@OpenWrt:~# kill -9 1170 1169 1167 1163
root@OpenWrt:~# ps aux |grep anyprogram
 1163 root         0 Z    [anyprogramd]
root@OpenWrt:~# kill -9 1163
root@OpenWrt:~# ps aux |grep anyprogram
 1163 root         0 Z    [anyprogramd]

A zombie is already dead, so you cannot kill it.僵尸已经死了,所以你不能杀死它。 To clean up a zombie, it must be waited on by its parent, so killing the parent should work to eliminate the zombie.要清理僵尸,它必须由其父级等待,因此杀死父级应该可以消除僵尸。 (After the parent dies, the zombie will be inherited by pid 1, which will wait on it and clear its entry in the process table.) If your daemon is spawning children that become zombies, you have a bug. (在父进程死后,僵尸进程将被 pid 1 继承,它会等待它并清除它在进程表中的条目。)如果你的守护进程正在产生变成僵尸的子进程,那么你就有了一个错误。 Your daemon should notice when its children die and wait on them to determine their exit status.你的守护进程应该注意到它的孩子何时死亡并wait他们确定他们的退出状态。

An example of how you might send a signal to every process that is the parent of a zombie (note that this is extremely crude and might kill processes that you do not intend. I do not recommend using this sort of sledge hammer):一个如何向作为僵尸父进程的每个进程发送信号的示例(请注意,这非常粗糙,可能会杀死您不想要的进程。我不建议使用这种大锤):

# Don't do this.  Incredibly risky sledge hammer!
kill $(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}')

您可以通过使用以下命令杀死其父进程来清理僵尸进程:

kill -HUP $(ps -A -ostat,ppid | awk '{/[zZ]/{ print $2 }')

I tried:我试过了:

ps aux | grep -w Z   # returns the zombies pid
ps o ppid {returned pid from previous command}   # returns the parent
kill -1 {the parent id from previous command}

this will work :)这将工作:)

Found it at http://www.linuxquestions.org/questions/suse-novell-60/howto-kill-defunct-processes-574612/http://www.linuxquestions.org/questions/suse-novell-60/howto-kill-defunct-processes-574612/找到它

2) Here a great tip from another user (Thxs Bill Dandreta): Sometimes 2) 这是来自另一个用户 (Thxs Bill Dandreta) 的一个很好的提示:有时

kill -9 <pid>

will not kill a process.不会杀死进程。 Run跑步

ps -xal

the 4th field is the parent process, kill all of a zombie's parents and the zombie dies!第四个字段是父进程,杀死所有僵尸的父母,僵尸就死了!

Example例子

4 0 18581 31706 17 0 2664 1236 wait S ? 0:00 sh -c /usr/bin/gcc -fomit-frame-pointer -O -mfpmat
4 0 18582 18581 17 0 2064 828 wait S ? 0:00 /usr/i686-pc-linux-gnu/gcc-bin/3.3.6/gcc -fomit-fr
4 0 18583 18582 21 0 6684 3100 - R ? 0:00 /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/cc1 -quie

18581 , 18582 , 18583 are zombies - 185811858218583是僵尸-

kill -9 18581 18582 18583

has no effect.没有效果。

kill -9 31706

removes the zombies.移除僵尸。

I tried我试过了

kill -9 $(ps -A -ostat,ppid | grep -e '[zZ]'| awk '{ print $2 }')

and it works for me.它对我有用。

有时无法杀死父 ppid,因此杀死僵尸 pid

kill -9 $(ps -A -ostat,pid | awk '/[zZ]/{ print $2 }')

On mac non of the above commands/instructions worked.在 mac 上,上述命令/说明均无效。 To remove zombie processes you can right click on docker-icon->troubleshot->clean/purge Data.要删除僵尸进程,您可以右键单击 docker-icon->troubleshot->clean/purge Data。

在此处输入图片说明

I do not dare to try above methods.我不敢尝试以上方法。

My solution is htop then detect which process have multiprocessing.spawn and kill -9 it.我的解决方案是 htop 然后检测哪个进程有 multiprocessing.spawn 并杀死 -9 它。

Combining a few answers here into an elegant approach where you confirm which process is gone zombie before killing it.将此处的一些答案组合成一种优雅的方法,您可以在杀死它之前确认哪个进程已成为僵尸。 Add the script to .bashrc / .zshrc and run the killZombie command.将脚本添加到.bashrc / killZombie .zshrc

killZombie() {
    pid=$(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}');
    if [ "$pid" = "" ]; then
        echo "No zombie processes found.";
    else
        cmd=$(ps -p $pid -o cmd | sed '1d');
        echo "Found zombie process PID: $pid";
        echo "$cmd";
        echo "Kill it? Return to continue… (ctrl+c to cancel)";
        read -r;
        sudo kill -9 $pid;
    fi
}

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

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