Coder Perfect

How to kill zombie process


I started my program (a daemon program) in the foreground and killed it with kill -9, however there is still a zombie left that I can’t kill with kill -9. How can you put an end to a zombie process?

How can I remove the zombie from the ps aux output if it’s a dead (previously killed) process?

root@OpenWrt:~# anyprogramd &
root@OpenWrt:~# ps aux | grep anyprogram
 1163 root      2552 S    anyprogramd
 1167 root      2552 S    anyprogramd
 1169 root      2552 S    anyprogramd
 1170 root      2552 S    anyprogramd
10101 root       944 S    grep anyprogram
root@OpenWrt:~# pidof anyprogramd
1170 1169 1167 1163
root@OpenWrt:~# kill -9 1170 1169 1167 1163
root@OpenWrt:~# ps aux |grep anyprogram
 1163 root         0 Z    [anyprogramd]
root@OpenWrt:~# kill -9 1163
root@OpenWrt:~# ps aux |grep anyprogram
 1163 root         0 Z    [anyprogramd]

Asked by MOHAMED

Solution #1

You can’t kill a zombie since it’s already dead. A zombie must be waited on by its parent in order to be cleaned up, hence killing the parent should work to eradicate the zombie. (When the zombie’s parent dies, pid 1 inherits it and waits for it to clear its entry in the process table.) You have an issue if your daemon is spawning children who turn into zombies. Your daemon should notice when its children die and wait on them to determine their exit status.

This is an example of how you might send a signal to every process that is the parent of a zombie (notice that this is incredibly primitive and may kill processes you don’t want to kill). This type of sledge hammer is not recommended):

# Don't do this.  Incredibly risky sledge hammer!
kill $(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}')

Answered by William Pursell

Solution #2

A zombie process can be cleaned up by killing its parent process with the following command:

kill -HUP $(ps -A -ostat,ppid | awk '{/[zZ]/{ print $2 }')

Answered by krishna murti

Solution #3

I tried:

ps aux | grep -w Z   # returns the zombies pid
ps o ppid {returned pid from previous command}   # returns the parent
kill -1 {the parent id from previous command}

This is going to work:)

Answered by Mohammad Rafiee

Solution #4 I found it.

2) Here’s an excellent suggestion from another user (thanks, Bill Dandreta): Sometimes

kill -9 <pid>

will not terminate a process Run

ps -xal

The parent process is the fourth field; kill all of a zombie’s parents and the zombie will die!


4 0 18581 31706 17 0 2664 1236 wait S ? 0:00 sh -c /usr/bin/gcc -fomit-frame-pointer -O -mfpmat
4 0 18582 18581 17 0 2064 828 wait S ? 0:00 /usr/i686-pc-linux-gnu/gcc-bin/3.3.6/gcc -fomit-fr
4 0 18583 18582 21 0 6684 3100 - R ? 0:00 /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/cc1 -quie

The numbers 18581, 18582, and 18583 are all zombies –

kill -9 18581 18582 18583

has no effect.

kill -9 31706

removes the zombies.

Answered by Sergio

Solution #5

I tried

kill -9 $(ps -A -ostat,ppid | grep -e '[zZ]'| awk '{ print $2 }')

It also worked for me.

Answered by Jeoffrey

Post is based on