问题描述
|
有没有办法杀死僵尸进程?我曾尝试调用
exit
杀死进程,甚至向进程发送SIGINT
信号,但似乎没有什么可以杀死它。我正在为Linux编程。
解决方法
僵尸进程已经死亡,因此无法将其杀死,只能将其收割,这必须由其父进程通过2来完成。在
SIGCHLD
的信号处理程序中,这通常称为child reaper
惯用语:
while (wait*(... WNOHANG ...)) {
...
}
, 这是我创建的杀死所有僵尸进程的脚本。它使用GDB调试器附加到父进程,并发送一个waitpid来杀死僵尸进程。这将使父母活着,并且只会杀死僵尸。
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 $1}\'`
zombieid=`echo $LINE | awk \'{print $2}\'`
verifyzombie=`echo $LINE | awk \'{print $3}\'`
# make sure this is a zombie file and we are not getting a Z from
# the command field of the ps -e -o ppid,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)\" >> /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 \"***********************************************************\"
请享用。
, 僵尸进程是其父进程尚未等待的进程ID(以及相关的终止状态和资源使用信息)。消除它的唯一方法是让它的父母等待它(有时,可以通过手动向父母发送ѭ4来实现,如果父母只是越野车,并且在比赛条件下错过了等待的机会),但是通常除非您强行终止父母,否则运气不佳。
编辑:另一种方法,如果您绝望而又不想杀死父母,则使用gdb附加到父母,并在僵尸孩子上强行调用waitpid
。
, 如果我没记错的话,杀死僵尸进程的父进程将使僵尸进程死亡。
使用ѭ9来获取正在运行的进程的漂亮层次树,其中显示了父/子关系。
, kill -17 ZOMBIE_PID
要么
kill -SIGCHLD ZOMBIE_PID
就像其他所有人说的那样,它可能会起作用,它正在等待父母打电话给“ 12”,因此除非父母死了而没有收获,并且由于某种原因卡在那儿,您可能不想杀死它。
, 请参见unix-faqs \“如何摆脱持久的僵尸进程?\”
您无法杀死僵尸,因为它们已经死了。但是,如果您有太多的僵尸,请杀死父进程或重新启动服务。
您可以尝试使用其pid杀死僵尸进程
kill -9 pid
请注意,kill -9不能保证杀死僵尸进程