This question already has answers here:

zombie process can't be killed
(6个答案)
我必须在LinuxC中杀死/清除僵尸进程。我只知道僵尸的PID。
我正在一个循环中创建几个僵尸进程:
int i = 0;
for (i; i<5; i++)
{
  system("(: & exec sleep 30) &"); // create zombie for 30 sec
}

我可以使用以下方法获取他们的PID编号:
system("ps aux | awk '{ print $8 " " $2 }' | grep -w Z");

但是如何使用pid来杀死/清除它们呢?
我将PID保存为变量和标准信号:
kill(PID,9)

根本不起作用,因为这个过程已经死了。
还有别的办法吗?请不要问我为什么我要创造僵尸来杀死他们。不是这样的。

最佳答案

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

享受吧。

关于c - 杀死僵尸进程,了解Linux C中的PID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13669597/

10-11 16:41