僵尸进程无法被杀死

发布于 2024-11-15 00:41:07 字数 82 浏览 2 评论 0原文

有没有办法杀死僵尸进程?我尝试过调用 exit 来终止进程,甚至向进程发送 SIGINT 信号,但似乎没有什么可以终止它。我正在为 Linux 编程。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南风几经秋 2024-11-22 00:41:07

僵尸进程已经死亡,因此它们无法被杀死,只能被收割,这必须由其父进程通过 wait*() 来完成。在 SIGCHLD 的信号处理程序中,这通常称为 child reaper 惯用语:

while (wait*(... WNOHANG ...)) {
    ...
}

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:

while (wait*(... WNOHANG ...)) {
    ...
}
ヅ她的身影、若隐若现 2024-11-22 00:41:07

这是我创建的用于杀死所有僵尸进程的脚本。它使用 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,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 "***********************************************************"

享受。

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 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.

#!/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,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.

僵尸进程是其父进程尚未等待的进程 ID(以及相关的终止状态和资源使用信息)。消除它的唯一方法是让其父级等待它(有时,如果父级只是有问题并且存在竞争条件而错过了父级,则可以通过手动向父级发送 SIGCHLD 来实现这一点)等待的机会)但通常你会运气不佳,除非你强行终止父母。

编辑:另一种方法,如果您绝望并且不想杀死父级,则可以使用 gdb 附加到父级并在僵尸子级上强制调用 waitpid

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.

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.

浮萍、无处依 2024-11-22 00:41:07

kill -17 ZOMBIE_PID

kill -SIGCHLD ZOMBIE_PID

可能会起作用,但就像其他人所说的那样,它正在等待父级调用 wait() 因此,除非父母没有收获就死了,并且由于某种原因卡在那里,否则你可能不想杀死它。

kill -17 ZOMBIE_PID

OR

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.

流年里的时光 2024-11-22 00:41:07

如果我没记错的话,杀死僵尸进程的父进程将使僵尸进程死亡。

使用 ps faux 获取正在运行的进程的良好分层树,显示父/子关系。

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.

毁梦 2024-11-22 00:41:07

请参阅 unix-faqs “我如何获得摆脱持续存在的僵尸进程吗?”

你无法杀死僵尸,因为它们已经死了。但如果僵尸太多,则终止父进程或重新启动服务。

您可以尝试使用其 pid 来杀死僵尸进程

kill -9 pid

请注意,kill -9 并不能保证杀死僵尸进程

See unix-faqs "How do I get rid of zombie processes that persevere?"

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

kill -9 pid

Please note that kill -9 does not guarantee to kill a zombie process

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文