How to Detect, Kill, and Prevent Zombie Processes in Linux
This article explains what zombie processes are in Unix/Linux, shows how to list and count them using ps and top, provides commands to terminate them by killing their parent or sending signals, and offers strategies such as ignoring SIGCHLD or double‑forking to prevent their creation.
In UNIX system terminology, a process that has terminated but whose parent has not yet waited for it is called a zombie.
In UNIX, when a child process ends while the parent has not called wait/waitpid or ignored SIGCHLD, the child becomes a zombie.
How to view zombie processes on Linux
You can list zombie processes with: ps -ef | grep defunct Or search for processes with state Z: ps -ef | grep Z The top command also shows zombies in the “Z” column, e.g.:
Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie
You can count zombies with:
ps -ef | grep defunct | grep -v grep | wc -lHow to kill zombie processes
Zombie processes cannot be killed directly; you must kill their parent. When the parent dies, the zombie becomes an orphan and is adopted by init (PID 1), which cleans it up. ps -e -o ppid,stat | grep Z | cut -d " " -f2 | xargs kill -9 Or:
kill -HUP $(ps -A -ostat,ppid | grep -e '^[Zz]' | awk '{print $2}')You can also write a custom shell script to automate this.
How to prevent zombie processes
Handling SIGCHLD is not mandatory, but for server processes that fork children to handle requests, not waiting can create zombies and waste resources. To avoid zombies, you can ignore SIGCHLD: signal(SIGCHLD, SIG_IGN); Alternatively, use a double‑fork technique: the intermediate child exits immediately, making the grandchild an orphan that init will reap.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
