What is a zombie process, and how do you get rid of it?
A zombie is a process that has finished but still has an entry in the process table because its parent has not read its exit status. It uses no CPU or memory beyond that entry. You fix it by making the parent call wait, restarting the parent, or killing the parent so init adopts and cleans it.
Explain it simply
Think of a student who has finished the exam and left, but whose name is still on the attendance register because the invigilator has not ticked it off. That name on the register is a zombie.
When a process exits, the kernel keeps a small record (PID and exit code) until its parent collects it with wait(). Until then the process shows as state Z in ps, with <defunct> next to the name.
A few zombies are normal and vanish quickly. Many of them point to a buggy parent that never reaps its children. They hold no memory or CPU, but they use up PIDs, and in extreme cases the PID limit.
You cannot kill a zombie with kill -9 because it is already dead. Options: send SIGCHLD to the parent (kill -s SIGCHLD PPID), fix or restart the parent, or kill the parent so that PID 1 adopts the zombies and reaps them.
Example
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /^Z/'
# 5120 5100 Z [worker] <defunct>
kill -s SIGCHLD 5100 # ask the parent to reapWhat to say in the interview
"A zombie has exited but its parent has not collected the exit status. It consumes no resources beyond a process table slot. I find the parent with ps, ask it to reap or restart it, because kill -9 on a zombie does nothing."
Common mistakes
- Trying kill -9 on the zombie itself.