Remove a zombie process from the process table

16,048

The only way to get rid of a zombie is to make its parent wait() so it can report its exit status. You can do that by sending SIGCHLD to the parent, assuming the parent is written properly.

If you have zombies it usually means the parent is NOT written properly (because the child already sent SIGCHLD to its parent when it died and became a zombie), so the next step is to kill the parent.
A tool like pstree (with the -p option) can show you the lineage of your zombies so you know which process is the parent.
When the parent dies the zombie will be adopted by init, which is always wait()ing for children to die, and will happily slay all the zombies it adopts.

If the parent process is actually init (PID 1) already you're in a situation that should never happen. You can try sending SIGCHLD to init, but you really shouldn't have to do that, and if that doesn't work your only recourse is to reboot because your system's init is broken and not doing its job.

(These are the "shotgun" options.)


Some more creative folks than I have also come up with this option if you want to avoid killing the parent process:

  1. Determine the zombie & parent processes' PIDS
    (For this example let's say the zombie is PID 3101 and the parent is PID 3100)
  2. Fire up gdb and attach to the parent:
    attach 3100
  3. Call waitpid for the zombie:
    call waitpid(3101,0,0)
  4. Detach from the parent (detach) and exit the debugger.

(This is a finely tuned sniper rifle.)

Share:
16,048

Related videos on Youtube

Jelle De Loecker
Author by

Jelle De Loecker

Updated on September 18, 2022

Comments

  • Jelle De Loecker
    Jelle De Loecker over 1 year

    I've got an annoying zombie process that gets adopted by init, and it won't go away. I've read there is a way to create a dummy process, attach the zombie as a child of that new process and then kill it, removing it from the process table.

    How would I do that, precisely?

    And yes, I've read most of this stuff:

    A zombie process is already dead, so it can't be killed.

    Or

    You should just reboot your system

    And

    Zombie processes don't use any resources, you should just let them be

    Unfortunately, lots of programs check the process table to see if an instance is already running, and will refuse to start a new one if there is an entry in the process table.

    And rebooting every time my SSHFS connection drops out, taking Sublime with it, is kind of silly.

    • Nathan C
      Nathan C about 10 years
      ...with a shotgun. ;)
    • HopelessN00b
      HopelessN00b about 10 years
      +1, because I looked at the main and saw "kill zombie"
  • Danila Ladner
    Danila Ladner about 10 years
    heh, great answer. "Mosin" rifle right there.
  • voretaq7
    voretaq7 about 10 years
    @DanilaLadner I wish I could take credit, but until I went on a little google hunt to see what skerit meant about "attaching the zombie as a child of a dummy process" the thought of using the debugger to force a waitpid() never occurred to me. I'm a terrible ex-programmer...
  • deFreitas
    deFreitas over 6 years
    call waitpid never returns for me