Killing zombie process

by LinuxTitli · 20 comments

Zombie process is an inactive computer process, according to wikipedia article, "...On Unix operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table, allowing the process that started it to read its exit status. In the term's colorful metaphor, the child process has died but has not yet been reaped..."

So how do I find out zombie process?

Use top or ps command:
# top
OR
# ps aux | awk '{ print $8 " " $2 }' | grep -w Z

Output:

Z 4104
Z 5320
Z 2945

How do I kill zombie process?

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 kill zombie process using PID obtained from any one of the above command. For example kill zombie proces having PID 4104:
# kill -9 4104

Please note that kill -9 does not guarantee to kill a zombie process (see below for more info).

How do I automate zombie process killing?

Write a script and schedule as a cron job.

See also:

What are these zombie processes that show up in ps? I kill them but they don't go away! by David Newall.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 20 comments… read them below or add one }

1 rdk 06.08.06 at 1:58 am

My friend is using something as follow (he calls it as linux zombies kill script.. LOL)

for each in `ps jauxww | grep Z | grep -v PID | awk ‘{print $3}’`; do for every in `ps auxw | grep $each | grep cron | awk ‘{print $2}’`; do kill -9 $every; done; done

Hope it will be useful to someone.

2 ldogan 09.19.06 at 8:31 pm

help me…

awk: cmd. line:1: .{print
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: (END OF FILE)
awk: cmd. line:2: syntax error
ERROR: Conflicting format options.

3 nixcraft 09.19.06 at 10:52 pm

Just copy and paste the code. I have fixed the css code problem.

4 lee colleton 10.03.06 at 5:23 pm

grepping for the “word” Z won’t show Zombies that are also session leaders (such as Xorg) which show up as Zs

recommend ending with “| grep Z”

5 James 03.27.07 at 3:54 pm

You should read the Wikipedia article more closely. There is no point sending a SIGKILL to a zombie process.

$ cat makezombie.c
#include
#include
#include
#include

int main(int argc, char *argv[])
{
pid_t child;

for (;;)
{
child = fork();
if (0 == child)
{
/* Whaddya know, I am the walrus^Wchild. Announce myself and
* exit immediately, making myself a zombie.
*/
fprintf(stdout, “Child PID is %ld\n”, (signed long int)getpid());
exit(0);
}
else if (child > 0)
{
/* I’m the parent. Loop without
* reaping the child.
*/
for (;;)
{
sleep(1);
}
}
else
{
/* fork() failed. Wait and try again. */
perror(“fork”);
sleep(1);
}
}
}
$ cc makezombie.c -o makezombie
$ ls -l makezombie
-rwxr-x— 1 youngman eng 9871 Mar 27 16:52 makezombie
$ ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2348 pts/2 00:00:00 ps
$ ./makezombie &
[1] 2351
$ Child PID is 2352
ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2351 pts/2 00:00:00 makezombie
2352 pts/2 00:00:00 makezombie
2353 pts/2 00:00:00 ps
$ kill -9 2352; echo $?
0
$ ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2351 pts/2 00:00:00 makezombie
2352 pts/2 00:00:00 makezombie
2356 pts/2 00:00:00 ps
$ kill 2351
$ ps
PID TTY TIME CMD
1769 pts/2 00:00:00 bash
2364 pts/2 00:00:00 ps
[1]+ Terminated ./makezombie
$

6 aditya 04.27.07 at 6:36 am

Plz tell me !
The difference between a thread and a process.
Plz give a detailed explanation.
I hope you will not let me down.
Thank you.

7 schwarz 07.27.07 at 2:16 pm

The first script has major issues with it. This is the one that I use

for each in `ps -ef | grep ” | grep -v PID | awk ‘{ print $3 }’`; do for every in `ps -ef | grep $each | grep -v cron | awk ‘{ print $2 }’`; do kill -9 $every; done; done

8 Pratik Kumar Mishra 12.05.07 at 6:36 am

thanks.
I Got my ans of zombie Process.

9 AYAK 01.30.08 at 12:26 pm

The thread is some thing like background job for the process smiler to project count from 1 till 100 and the main purpose of the project is get each number value.
eg in our daily life is some programs can’t do any thing until it’s main job done like some sort of Anti Virus’s when you tried to do another job the program dosen’t respond to any command in the main time i’s in the same process but it take her own mem space :D
i hope i was any useful for you :D

10 Don 06.11.08 at 8:22 pm
#!/usr/bin/bash
#
# zombie_slayer [OPTIONS]
#
# -d
#   Just display the zombie processes and exit
#
# -h | help
#   Display help
#
# Default is to show processes and the ask what to kill (exits if any errors when killing processes)
#
function Z_Display
{
    echo ""
    echo "Proc - PID - PPID - State - User"
    UNIX95= ps -eo comm,pid,ppid,state,user | awk '$4 ~ /Z/ { print $1,$2,$3,$4,$5 }'
    echo ""
}
function Z_Kill
{
    read -p "Enter PPID to kill or 'exit' : " SLAY_PPID
    if [ "$SLAY_PPID" = "exit" ] || [ "$SLAY_PPID" = "" ]
    then
        exit
    fi
    ps -p $SLAY_PPID | grep -q $SLAY_PPID
    if [ $? -eq 0 ]
    then
        UNIX95= ps -o pid,user,state,comm -p $SLAY_PPID | \
        awk '$1 ~ /^[0-9]*$/ { print "The program " $4 " with PID " $1 " is being run by the user " $2 " and is currently in state " $3 }'
        read -p "Are you sure you want to kill PID $SLAY_PPID ? Y|N : " COMMIT_KILL
        if [ "$COMMIT_KILL" = "Y" ] || [ "$COMMIT_KILL" = "y" ]
        then
            kill -9 $SLAY_PPID
            if [$? -eq 0]
            then
                echo ""
                read -p "Killed PID $SLAY_PPID. Run again? Y/N : " GO_AGAIN
            fi
        else
            Z_Kill
        fi
    else
        echo "Invalid PID. Try again."
        echo ""
        Z_Kill
    fi
}
function help
{
    echo "Usage: zombie_slayer [-h | help -d]"
    echo "-d just display zombie processes and exit"
    echo "-h | help : This help"
    exit
}

if [ "$1" = "-h" ] || [ "$1" = "help" ]
then
    help
fi

if [ "$1" = "-d" ]
then
    Z_Display
else
    Z_Display
    Z_Kill
fi
if [ "$GO_AGAIN" = "Y" ] || [ "$GO_AGAIN" = "y" ]
then
    Z_Display
    Z_Kill
fi
echo ""
11 Don 06.17.08 at 5:09 pm

Updated and tested with HP-UX 11i, Solaris 10, Debian, RHEL4 and SUSE 10

#!/usr/bin/bash
#
# Zombie Slayer
#
# zombie_slayer [OPTIONS]
#
# -d
#   Just diplay the zombie processes and exit
#
# -h | help
#   Display help
#
# Default is to show processes and the ask what to kill
#

function Z_Display
{
    echo ""
    echo "PID - PPID - State - User - Proc"
    UNIX95= ps -eo pid,ppid,state,user,comm | awk 'BEGIN { count=0 } $3 ~ /Z/ { count++; print $1,$2,$3,$4,$5 } END { print "\n" count " Zombie(s) to slay." }'
    echo ""
}

function Z_Kill
{
    read -p "Enter PPID to kill or 'exit' : " SLAY_PPID
    if [ "$SLAY_PPID" = "exit" ] || [ "$SLAY_PPID" = "" ]
    then
        exit
    fi
    ps -p $SLAY_PPID | grep -q $SLAY_PPID
    if [ $? -eq 0 ]
    then
        UNIX95= ps -o pid,user,state,comm -p $SLAY_PPID | \
        awk '$1 ~ /^[0-9]*$/ { print "The program " $4 " with PID " $1 " is being run by the user " $2 " and is currently in state " $3 }'
        read -p "Are you sure you want to kill PID $SLAY_PPID ? Y|N : " COMMIT_KILL
        if [ "$COMMIT_KILL" = "Y" ] || [ "$COMMIT_KILL" = "y" ]
        then
            kill -9 $SLAY_PPID
            echo ""
            read -p "Killed PID $SLAY_PPID. Run again? Y/N : " GO_AGAIN
        else
            Z_Kill
        fi
    else
        echo "Invalid PID. Try again."
        echo ""
        Z_Kill
    fi
}

function help
{
    echo "Usage: zombie_slayer [-h | help -d]"
    echo "-d just display zombie processes and exit"
    echo "-h | help : This help"
    exit
}

if [ "$1" = "-h" ] || [ "$1" = "help" ]
then
    help
fi

if [ "$1" = "-d" ]
then
    Z_Display
else
    Z_Display
    Z_Kill
fi
if [ "$GO_AGAIN" = "Y" ] || [ "$GO_AGAIN" = "y" ]
then
    Z_Display
    Z_Kill
fi
echo ""
12 none 01.30.09 at 2:50 pm

Quick ‘n dirty hack:

ps ax | awk ‘{ if ($NF == “”) print $1 }’ | xargs kill -9

13 none 01.30.09 at 2:55 pm

I guess the html is stripped, hope at least one works, I mean $NF == defunct between “less than” and “greater than”:

ps ax | awk ‘{ if ($NF == “&#60defunct&#62″) print $1 }’ | xargs kill -9

ps ax | awk ‘{ if ($NF == “\”) print $1 }’ | xargs kill -9

14 foo 03.10.09 at 9:42 pm

You need to send the parent process a SIGCHLD or kill it

ps -eo pid,ppid,user,args,stat –sort stat | grep Z | awk ‘{ print $2 }’ | sort -u

gives you the parent process id(s)

kill -s SIGCHLD
or
kill

15 Paul 04.24.09 at 4:39 pm

First things first, I am amazed at the amount of time so many people have put into dealing with such a non-issue. Zombie processes are dead, they consume nearly zero system resources. The only time you should be concerned is if you have a large number of them and said number is continuing to grow, in which case getting rid of the zombies is not your problem. You need to find out why you have them in the first place. Removing them is like taking a cold tablet, you are deling with the symptom, not the real problem.

That said, you should never, ever be using ‘kill -9′, that is an absolute last-ditch resort for dealing with a system that is almost non-functional or in some other critical state. Having a few zombie processes tying up nearly no resources at all is not a critical state, and using kill -9 is like chasing a mouse with a heavy hammer; you’re not likely to get the mouse, but you may do extensive damage to everything around you in the process.

So, what to do? find out what is spawning these processes, and why it is not shutting them down when they exit. See if there is a patch that fixes the issue. Make a report to the maintainers of the package. Restart the parent process. Or to reiterate, find out the cause of the problem and fix that.

16 Lesh 07.09.09 at 12:06 pm

ps -ef | grep defunct | awk ‘{ print $3 }’ | xargs kill -9

17 steveus christ 08.13.09 at 4:19 pm

if u want to kill a zombie. just shoot it till it stops moving. its that simple. u should probably shoot the forhead. or the noggin. use a machete or a shotgun seem to work well. when the zombie apocolyps comes. set up traps and execute them. at costs dont let ur zombie bite u.

18 Don 08.13.09 at 4:55 pm

I know this is old but WTH -

I wrote the script above because we had several developers on a group of HPUX boxes testing a complicated MPE ported application. In many situations runaway processes would create zombie children which would alert us to problematic code. This script would tell me what process created them and who the developer was who kicked it off. Very helpful to me, and I thought that someone, somewhere might benefit from it. Since it is still here and ya’ll are finding it when you search for “how to Kill Zombie processes” it looks like maybe I was right :) Maybe someone learned some basic scripting skills too ;) Isn’t that what the internet is for?

19 tangbq 12.09.09 at 6:14 am

ps aux | awk ‘{ print $8 ” ” $2 }’ | grep ‘Z’ |awk ‘{print $2}’ |xargs kill -9

20 Yo 12.17.09 at 1:40 pm

Nice comments by Paul and Steveus
I even like a youtube video titled as “How to Kill a Zombie”
Do search and watch it, Its a must lesson for all of the geeks here !!!
Oops I forgot to say – Nice document by Nixcraft and best feature is I can save it as PDF.
Thanks!!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: