Zombie and orphan processes

Video 126 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:46, with English and Hindi audio.

Sometimes the process list shows a row marked defunct, and KILL changes nothing. That row is either a zombie or an orphan, and the two need different fixes. Both cases are about parent processes, the programs that started other programs. This article explains how a child process ends, what a zombie and an orphan are, how the kernel hands an orphan to systemd, and how to find the parent of a zombie so you fix the right program.

What you will learn

A defunct row, and how parents and children work

On a warehouse server, the process list shows a row marked defunct. The engineer sends KILL to it, and nothing happens. That row is either a zombie or an orphan. Each case needs a different fix, and the difference is about parents.

Almost every process was started by another process. That starter is its parent, and the child is the process it started. The kernel keeps the link between them, and a child can report its end only through its parent.

When a child process finishes, it exits and leaves a small exit status behind, saying how it ended. The parent should call wait, which collects that status and removes the leftover record from the process table.

A zombie: ended, but not collected

If the parent never waits, the finished child stays as a zombie. A zombie holds only a table entry, and it cannot run. Sending KILL to it does nothing, because the process has already ended. Only its parent can clear the record. A parent that never waits can pile up zombies, and each one takes a slot in the process table, which has a limit.

An orphan: parent gone, child still running

Now suppose a monitor script starts two checks, and the script crashes while one check is still running. That check is an orphan, because its parent has gone. The kernel hands the orphan to systemd, which is process ID 1. The orphan keeps running, and systemd collects it when it ends. Systemd collects the status of the children it adopts, so orphans do not turn into zombies for long.

Finding zombies and their parent

List processes and keep the rows marked defunct
ps -eo pid,ppid,stat,cmd | grep defunct

Note: A zombie shows the word defunct and the state letter Z. The second column is the parent's process ID.

Print only the parent's process ID of process 4321
ps -o ppid= -p 4321

The parent is the program that fails to collect its children, so this tells you which program to look at.

Stop the failing parent process 4300
kill -TERM 4300

Note: Stopping the parent is the usual cure. Once it is gone, the zombies are handed to systemd, which collects them.

What goes wrong with zombies

Try it yourself

You may find no zombies on a healthy machine, and that is normal. The first two steps only read the process table. Run the last step only on a parent process that you own and have confirmed is failing.

  1. List the processes and keep the rows marked defunct. A zombie has the word defunct and the state letter Z.

    ps -eo pid,ppid,stat,cmd | grep defunct

    What you should see: Each zombie row shows the word defunct, the state letter Z, and its parent's process ID in the second column.

  2. Print only the parent's process ID of process 4321. Use a zombie's ID from the previous step in place of 4321.

    ps -o ppid= -p 4321

    What you should see: The shell prints only the parent's process ID, so you know which program must collect the child.

  3. Stop the failing parent, using its ID from the previous step in place of 4300. Only do this on a process you are sure is failing.

    kill -TERM 4300

    What you should see: Once the parent is gone, the zombies are handed to systemd, which collects them.

Terms

Zombie process
A process that has ended but whose parent has not yet collected its exit status, shown as defunct with state Z.
Orphan process
A process still running after its parent has gone, which the kernel hands to systemd.
Parent process
The process that started another process, and that must collect the child's exit status when it ends.

Check yourself

Why does KILL do nothing to a zombie?

The zombie has already ended. Only its parent can collect its exit status and clear its record.

What happens to an orphan when its parent has gone?

The kernel hands it to systemd, which is process ID 1. The orphan keeps running, and systemd collects it when it ends.

Where should you look when zombies pile up?

At the parent process, because it is the program that fails to collect its children.

Previous: Foreground, background, and surviving a logout with nohup. Next: What is inside the proc folder for a process.