When a parent process is terminated without notifying the change, its child process becomes the zombie and is named a zombie process (defunct). The child process does not get any termination signal, which is why it stays in the memory. Every process in Linux has to inform its parent process before termination otherwise, it stays in the memory until it notifies.
This post will address the method to find and kill a zombie process in Linux.
Reasons That Invoke a Zombie Process
Following are the major causes of the zombie process:
- The parent process is unable to call the wait() function when its child process is in running state which ignores the SIGCHLD (signal that the system sends). The process should wait for the child process to terminate by calling the wait() function else the child process will leave in the zombie state.
- The execution of the parent process is affected by other applications/software.
The zombie process is not risky as it only occupies some of the memory. But the particular process id that this process is using cannot be used until it is released. In some scenarios, if the user has a bunch of zombie processes, it can create a problem for other processes to run.
How to Find and Kill the Zombie Process on Linux?
Follow the given steps to find and kill the zombie process in Linux.
Step 1: List the Zombie Process
First, list down the zombie process using process; the identification of the zombie process can be made by checking the ”Z” in the “STAT” column as shown below:
$ ps aux | egrep "Z|defunct"
In the above image, we have one zombie process, having PID “4911”.
Step 2: Get the Parent Process ID
The next step is to get the parent process id of the zombie process so that the parent is notified about the status of its child process. In the below command, the process ID is obtained from step 1 to display the child/zombie process ID. Use the following command to get the parent process id:
$ ps -o ppid= 4911
The zombie process id is 4910.
Step 3: Kill the Zombie Process By Notifying the Parent
After that, send the “SIGCHLD” (Termination Signal) signal to the parent process to inform the termination of the child process:
$ kill -s SIGCHLD 4910
The zombie process is killed.
Note: The “SIGCHLD” does not terminate the child process. It’s a signal that sends to the parent process.
Verify the Result
To verify the zombie process has been killed, run the above command that we have used for listing down the zombie process:
$ ps aux | egrep "Z|defunct"
The zombie process listed in step 2 has been killed.
Step 4 (Optional): Kill the Parent Directly
If the above command does not kill the zombie process, the user can consider the following command:
$ kill -9 <Parent ID>
The zombie process will be killed.
To find and kill the zombie process on Linux, list it down, and notify its parent process. If the zombie process is still not killed after notifying its parent process, then again, kill the parent process. After that, kill the zombie process.
This write-up has illustrated the method to find and kill the zombie process on Linux.