Zombie processes are a common problem in Linux systems that can cause performance issues and even sometimes system crashes.
These processes are created when a child process finishes execution, but its parent process fails to read its exit status, leaving it in a zombie state
.
In this article, we will explore what zombie processes are, how to identify them, and most importantly, how to clean them up to keep your Linux system running smoothly.
What is a zombie process
Zombie processes on Linux are sometimes also referred to as defunct or dead processes. They’re processes that have completed their execution, but their entries are not removed from the process table.
On Linux, when a process completes its job, the Linux kernel notifies the exiting process parent by sending the SIGCHLD
signal. The parent then executes the wait()
system call to read the status of the child process and reads its exit code.
This also cleans up the entry of the child process from the process table, and hence, the process finishes. However, if a parent process isn’t programmed to execute the wait() system call on the creation of the child process, proper cleanup doesn’t happen.
In such cases, the parent process cannot monitor the state changes of the child processes, and eventually, it ignores the SIGCHLD signal.
This causes the zombie state of the finished process to stay in the process table, and hence it appears in the process list as a zombie process.
Another case of interest is when a parent process is unable to handle or receive the SIGCHLD signal from the child process. Such cases also lead to zombie creation.
Identification of a Zombie Process
Linux maintains a process table of all the processes running, along with their states. Let’s briefly overview the various process states:
- Running (R): These processes are currently running or runnable.
- Waiting (S/D): These are the processes that are waiting for an event or a resource.
- Stopped (T): These are the processes that have been stopped.
- Zombie (Z): When a process finishes its task, it releases the system resources it was using and cleans up its memory. However, its entry from the process table is not removed, and its status is set as EXIT_ZOMBIE.
So, we can identify the list of zombies using the ps
command:
As observed from the output, the Zs
in the STAT column can be used to identify the zombies.
Let’s further filter the output based on the Z process state using the awk
command:
We can also list and show the zombie process using the top
command:
Here, along with the other details, we can also see the number of zombie processes in the summary at the top of the output.
Cleaning Up a Zombie Process
A zombie process cannot really be killed since it’s already dead. However, there are a few workarounds we can use to clean up a zombie process.
We can manually send the SIGCHLD signal to the parent of a zombie process. It will intimate the parent to trigger the wait() system call, which will clean up the defunct child process from the process table.
Let’s find the parent ID of our defunct process:
This lists the STAT column, process id and parent process id of zombie processes.
Next, let’s send the SIGCHLD signal to the parent process using the kill
command:
kill -s SIGCHLD 552904
However, it isn’t really guaranteed that sending the SIGCHLD signal to the parent will kill a zombie process. It works only in cases where parent processes can handle the SIGCHLD signals.
If the method discussed in the previous section is unable to clear the defunct process, we should consider killing its parent process:
kill -9 552904
Here, 552904
is the parent ID of our defunct process with PID 553204
.
However, killing a parent can affect all its child processes. Hence, we should exercise extra caution and must identify the impact before killing a parent process.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts or opinions below in the comments section.
You can have more of my content by following me on LinkedIn • GitHub or Medium
Wrap up
In conclusion, zombie processes can be a nuisance on Linux systems, but they can be cleaned up using various methods. Manually sending the SIGCHLD signal to the parent process can often do the job, but if that fails, killing the parent process may be necessary.
However, it's important to exercise caution when killing a parent process, as it can impact all its child processes. By understanding what zombie processes are and how to identify and clean them up, Linux administrators can keep their systems running smoothly.