15 June 2018

Linux: How To Kill A Zombie Process

A "Zombie Process" is a Child process who exited unexpectedly before the Parent process, so the Parent process does not grab the status of child process using wait() or waitpid() system calls. This means that the Child process is in "Zombie" state. Zombies and Orphan processes are not the same thing: While a Zombie is a defunct Child Process, the Orphan is a process whose Parent process has been killed and inherited by init process.

When a process dies, it's child processes all become children of process number 1, which is the INIT process. Init is always waiting for children to die, so that they don't remain as zombies. If you have zombie processes it means those zombies have not been waited for by their parent (look at PPID displayed by ps -l). You have three choices: Fix the parent process (make it wait); kill the parent; or live with it.

Zombies can be identified in the output from the Unix / Linux ps command by the presence of a "Z" in the STAT column. Zombies that exist for more than a short period of time typically indicate a bug in the parent program.

You have two possible solutions to terminate a zombie process:

a) Reboot the Machine

The hard way. You'll have to save your work, close all apps, browsers, etc and reboot the machine. After the reboot, there will be no more zombies in your system - you can check using the top command:

top - 08:14:57 up 3 days, 23:14,  1 user,  load average: 0,13, 0,66, 0,89
Tasks: 207 total,   1 running, 206 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0,8 us,  0,4 sy,  0,0 ni, 98,7 id,  0,1 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem :  8126456 total,  1265528 free,  4152596 used,  2708332 buff/cache
KiB Swap:  8344572 total,  8338428 free,     6144 used.  3530668 avail Mem 

 

b) Trace the Zombie Process and Terminate it

To terminate a Zombie process, you have to kill it's parent process. Here's how to find the Zombie:

morbid@inferno:~$ ps aux | grep 'Z'

The process with STAT = 'Z' is your zombie.

USER       PID     %CPU %MEM  VSZ    RSS TTY      STAT START   TIME COMMAND
usera      12581   0.0  0.0   7628   992 pts/2    S+   19:40   0:00 grep --color=auto Z
usera      17844   0.0  0.0      0     0 ?           Jun14   0:00 [sd_cicero] <defunct>


Then, find the zombie's parent:

morbid@inferno:~$ pstree -p -s 19652

Will give you:

systemd(1)───sd_cicero(17843)───sd_cicero(17844)

Kill the parent process and the zombie will disappear:

morbid@inferno:~$ kill -9 17843

top - 08:14:57 up 3 days, 23:14,  1 user,  load average: 0,13, 0,66, 0,89
Tasks: 207 total,   1 running, 206 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0,8 us,  0,4 sy,  0,0 ni, 98,7 id,  0,1 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem :  8126456 total,  1265528 free,  4152596 used,  2708332 buff/cache
KiB Swap:  8344572 total,  8338428 free,     6144 used.  3530668 avail Mem

No comments:

Post a Comment