Linux 僵尸进程不能被杀死
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6335730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
zombie process can't be killed
提问by user12707
Is there a way to kill a zombie process? I've tried calling exit
to kill the process and even sending SIGINT
signal to the process, but it seems that nothing can kill it. I'm programming for Linux.
有没有办法杀死僵尸进程?我试过调用exit
杀死进程,甚至向进程发送SIGINT
信号,但似乎没有什么可以杀死它。我正在为 Linux 编程。
采纳答案by ninjalj
Zombie processes are already dead, so they cannot be killed, they can only be reaped, which has to be done by their parent process via wait*()
. This is usually called the child reaper
idiom, in the signal handler for SIGCHLD
:
僵尸进程已经死了,所以它们不能被杀死,它们只能被收割,这必须由它们的父进程通过wait*()
. 这通常称为child reaper
习语,在信号处理程序中用于SIGCHLD
:
while (wait*(... WNOHANG ...)) {
...
}
回答by Dario Russo
if I recall correctly, killing the parent of a zombie process will allow the zombie process to die.
如果我没记错的话,杀死僵尸进程的父进程将使僵尸进程死亡。
use ps faux
to get a nice hierarchical tree of your running processes showing parent/child relationships.
用于ps faux
获得显示父/子关系的正在运行的进程的良好分层树。
回答by R.. GitHub STOP HELPING ICE
A zombie process is a process id (and associated termination status and resource usage information) that has not yet been waited for by its parent process. The only ways to eliminate it are to get its parent to wait for it (sometimes this can be achieved by sending SIGCHLD
to the parent manually if the parent was just buggy and had a race condition where it missed the chance to wait) but usually you're out of luck unless you forcibly terminate the parent.
僵尸进程是其父进程尚未等待的进程 ID(以及相关的终止状态和资源使用信息)。消除它的唯一方法是让它的父级等待它(有时这可以通过SIGCHLD
手动发送给父级来实现,如果父级只是马车并且有一个竞争条件,它错过了等待的机会)但通常你'除非你强行终止父母,否则运气不好。
Edit:Another way, if you're desperate and don't want to kill the parent, is to attach to the parent with gdb and forcibly call waitpid
on the zombie child.
编辑:另一种方法,如果你绝望并且不想杀死父母,是用gdb附加到父母并强行调用waitpid
僵尸孩子。
回答by Fredrik Pihl
See unix-faqs"How do I get rid of zombie processes that persevere?"
请参阅unix-faqs“如何摆脱顽固的僵尸进程?”
You cannot kill zombies, as they are already dead. But if you have too many zombies then kill parent process or restart service.
你不能杀死僵尸,因为它们已经死了。但是如果你有太多的僵尸然后杀死父进程或重新启动服务。
You can try to kill zombie process using its pid
您可以尝试使用其 pid 杀死僵尸进程
kill -9 pid
Please note that kill -9 does not guarantee to kill a zombie process
请注意,kill -9 并不能保证杀死僵尸进程
回答by loosecannon
kill -17 ZOMBIE_PID
kill -17 ZOMBIE_PID
OR
或者
kill -SIGCHLD ZOMBIE_PID
kill -SIGCHLD ZOMBIE_PID
would possibly work, bu tlike everyone else said, it is waiting for the parent to call wait()
so unless the parent dies without reaping, and it got stuck there for some reason you might not want to kill it.
可能会起作用,但就像其他人说的那样,它正在等待父级调用wait()
,除非父级死亡而没有收获,并且由于某种原因您可能不想杀死它而卡在那里。
回答by Mitch Milner
Here is a script I created to kill ALL zombie processes. It uses the GDB debugger to attach to the parent process and send a waitpid to kill the zombie process. This will leave the parent live and only slay the zombie.
这是我创建的一个脚本来杀死所有僵尸进程。它使用 GDB 调试器附加到父进程并发送一个 waitpid 来杀死僵尸进程。这将使父母活着,只杀死僵尸。
GDB debugger will need to be installed and you will need to be logged in with permissions to attach to a process. This has been tested on Centos 6.3.
需要安装 GDB 调试器,并且您需要登录并具有附加到进程的权限。这已经在 Centos 6.3 上测试过了。
#!/bin/bash
##################################################################
# Script: Zombie Slayer
# Author: Mitch Milner
# Date: 03/13/2013 ---> A good day to slay zombies
#
# Requirements: yum install gdb
# permissions to attach to the parent process
#
# This script works by using a debugger to
# attach to the parent process and then issuing
# a waitpid to the dead zombie. This will not kill
# the living parent process.
##################################################################
clear
# Wait for user input to proceed, give user a chance to cancel script
echo "***********************************************************"
echo -e "This script will terminate all zombie process."
echo -e "Press [ENTER] to continue or [CTRL] + C to cancel:"
echo "***********************************************************"
read cmd_string
echo -e "\n"
# initialize variables
intcount=0
lastparentid=0
# remove old gdb command file
rm -f /tmp/zombie_slayer.txt
# create the gdb command file
echo "***********************************************************"
echo "Creating command file..."
echo "***********************************************************"
ps -e -o ppid,pid,stat,command | grep Z | sort | while read LINE; do
intcount=$((intcount+1))
parentid=`echo $LINE | awk '{print }'`
zombieid=`echo $LINE | awk '{print }'`
verifyzombie=`echo $LINE | awk '{print }'`
# make sure this is a zombie file and we are not getting a Z from
# the command field of the ps -e -o ppid,pid,stat,command
if [ "$verifyzombie" == "Z" ]
then
if [ "$parentid" != "$lastparentid" ]
then
if [ "$lastparentid" != "0" ]
then
echo "detach" >> /tmp/zombie_slayer.txt
fi
echo "attach $parentid" >> /tmp/zombie_slayer.txt
fi
echo "call waitpid ($zombieid,0,0)" >> /tmp/zombie_slayer.txt
echo "Logging: Parent: $parentid Zombie: $zombieid"
lastparentid=$parentid
fi
done
if [ "$lastparentid" != "0" ]
then
echo "detach" >> /tmp/zombie_slayer.txt
fi
# Slay the zombies with gdb and the created command file
echo -e "\n\n"
echo "***********************************************************"
echo "Slaying zombie processes..."
echo "***********************************************************"
gdb -batch -x /tmp/zombie_slayer.txt
echo -e "\n\n"
echo "***********************************************************"
echo "Script complete."
echo "***********************************************************"
Enjoy.
享受。