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.

- 6,875
- 4
- 37
- 56

- 294
- 1
- 5
- 12
-
2Oh, I had forgotten to make the obvious joke: the way to kill a zombie is a shoot in the head. – ninjalj Jun 13 '11 at 20:36
-
[When will kill PID fail?](http://serverfault.com/questions/276557/when-will-kill-pid-fail/276558#276558) – Prince John Wesley Jun 14 '11 at 03:18
-
See also [What does reaping children imply?](https://stackoverflow.com/questions/58885831) – Steve Summit Apr 04 '23 at 11:25
6 Answers
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 ...)) {
...
}

- 42,493
- 9
- 106
- 148
-
20I got to use `zombie`, `dead`, `killed` and `child reaper` in this answer :) – ninjalj Jun 13 '11 at 20:19
-
This code is incorrect. If you put that in your `SIGCHLD` handler, you risk blocking and never returning. – R.. GitHub STOP HELPING ICE Jun 13 '11 at 20:41
-
@R..: you're supposed to use one of the `wait()` variants that support `WNOHANG` – ninjalj Jun 13 '11 at 20:45
-
10You can also have the parent process ignore the child status via `signal(SIGCHLD, SIG_IGN)`. The child process goes away (leaves no zombie) as soon as it exits because the parent has registered a lack of interest in the well being of its children. – unpythonic Jun 13 '11 at 23:17
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.

- 86
- 1
-
Didn't run on Ubuntu 14.04: `warning: /tmp/zombie_slayer.txt: No such file or directory`. – Fernando Correia Feb 19 '16 at 18:54
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.

- 208,859
- 35
- 376
- 711
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.

- 7,683
- 3
- 32
- 43
-
2but i think this is wrong, you might have to send that to the parent, idk – loosecannon Jun 13 '11 at 20:19
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.

- 80
- 1
- 8
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

- 44,604
- 7
- 83
- 130
-
7Not only does it not guarantee to kill a zombie process. `kill -9` **can never** kill a zombie process. – R.. GitHub STOP HELPING ICE Jun 13 '11 at 20:42