About nixCraft

Topics

Killing zombie process

Posted by Vivek Gite [Last updated: September 19, 2006]

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.

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. rdk Says:

    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 Says:

    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 Says:

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

  4. lee colleton Says:

    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 Says:

    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 Says:

    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 Says:

    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 Says:

    thanks.
    I Got my ans of zombie Process.

  9. AYAK Says:

    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 Says:
    #!/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 Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.