> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Zombie vs Orphan Processes

> Explains zombie and orphan processes, how zombies are unreaped exited children, orphans are reparented to PID 1, and remedies including using an init like tini in containers.

If `ps` shows a process with status `defunct`, that entry is a zombie process.

Example:

```bash theme={null}
root@server:~$ ps aux | grep myapp
USER       PID %CPU %MEM STAT COMMAND
root      4821  0.0  0.0 Z    [myapp] <defunct>

root@server:~$ kill -9 4821
# ...still there.
```

The `defunct` (or `Z`) state means the process has already exited — it's dead — but its minimal bookkeeping entry (exit status, PID, and resource accounting) remains in the kernel process table because the parent has not yet collected that information.

How this works

* When a process exits, the kernel keeps the child's exit status and basic accounting until the parent calls a wait-family syscall (`wait()`, `waitpid()`, etc.). The parent retrieving the child's status is called reaping.
* A zombie is an exited child that has not been reaped. It uses no CPU and almost no memory — just a tiny process table entry.
* Because the process is already dead, sending signals (including `kill -9`) has no effect.

![An infographic explaining zombie processes: a child process has exited but remains listed because the parent didn't call wait(), shown as STATUS "Z" in a process table and illustrated with a ghost labeled "This is the zombie."](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Advanced-Concepts/Zombie-vs-Orphan-Processes/zombie-process-status-z-ghost.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=2b742c0edb09d07ec7c410074ce8c08f)

Diagnosing zombies

* Find the parent PID (PPID) and inspect the parent process to see why it's not reaping children.
* Useful command:

```bash theme={null}
# Show pid, ppid, stat, and command for a specific PID
ps -o pid,ppid,stat,cmd -p 4821
```

If the parent is buggy (for example, it never calls `wait()`), it will accumulate zombies. If that parent process exits, the kernel reparents those entries to PID 1 (init/systemd). PID 1 will reap children when they exit, clearing the zombie entries.

![A dark-themed slide explaining that "kill-9 does nothing" and advising to "Fix the parent," with colored boxes showing a parent process calling wait(), the parent dying, and PID 1 (init/systemd) reaping the child automatically. The diagram highlights that signals are for live processes and shows the practical flow.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Advanced-Concepts/Zombie-vs-Orphan-Processes/kill-9-fix-parent-pid1-reap.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=6ff30863937bb453de2bd9db07af189e)

Orphan processes

* An orphan is a live child whose parent died earlier. The kernel immediately reparents that live child to PID 1, which will wait on it when it exits.
* Orphans continue running normally; they are not zombies.

![A black-background diagram titled "The Orphan" showing a crossed-out parent process, a surviving child process, and an arrow to a box labeled "PID 1 adopts it." The caption explains PID 1 will wait on the child when it exits.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Advanced-Concepts/Zombie-vs-Orphan-Processes/orphan-process-pid1-adopts-diagram.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=a3f505118d1d303b0d260ece13f97bd7)

Quick comparison

| Situation | State | What it means | Who reaps it |
| -: | :-: | - | :- |
| Zombie | `Z` / `defunct` | Child exited but parent hasn't called `wait()` to collect exit status | The parent that spawned it (or PID 1 if the parent dies before reaping) |
| Orphan | Running | Parent died, child still running and is reparented to PID 1 | PID 1 will reap it when it eventually exits |

When zombies matter

* A few zombies are usually harmless. The kernel holds only tiny bookkeeping.
* A large accumulation of zombies can exhaust the PID namespace and indicate a buggy parent process that never reaps children.
* In container environments, the main application often runs as PID 1. If that program does not reap child processes, zombies can build up inside the container.

Containers and reaping

* For containers, use a tiny init (like `tini`) as PID 1 to correctly reap children. Docker provides a convenience flag to run an init process:

```bash theme={null}
# Run a container with a tiny init process (tini) that will reap zombies
docker run --init your-image
```

* See Docker run reference: [https://docs.docker.com/engine/reference/run/#specify-an-init-process--init](https://docs.docker.com/engine/reference/run/#specify-an-init-process--init)
* tini project: [https://github.com/krallin/tini](https://github.com/krallin/tini)

Summary

* Zombie: process has exited but remains listed until its parent reaps it.
* Orphan: process is still running after its parent dies; PID 1 adopts it.
* Fix the parent (or restart it) to clear zombies; in containers, run an init as PID 1 if your app doesn't reap children.

If you need to debug zombies, find the PPID of the defunct process (`ps -o pid,ppid,stat,cmd -p <PID>`) and inspect or fix the parent. If the parent can't be fixed immediately, restarting or killing it will cause PID 1 to adopt and reap the exited children.

Links and references

* Docker run reference: [https://docs.docker.com/engine/reference/run/#specify-an-init-process--init](https://docs.docker.com/engine/reference/run/#specify-an-init-process--init)
* tini (tiny init for containers): [https://github.com/krallin/tini](https://github.com/krallin/tini)
* Process management and wait(2) manual: [https://man7.org/linux/man-pages/man2/wait.2.html](https://man7.org/linux/man-pages/man2/wait.2.html)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/5de176de-dc0d-4961-bac5-a735e7d70eda/lesson/554a0b28-ba7e-423b-9caa-61e932019eea)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.