In Linux systems, zombie processes are special processes that have been terminated but still remain in the system. Although zombie processes do not consume many resources, if there are too many, they may cause system resource exhaustion. This article will introduce how to correctly remove zombie processes to ensure the normal operation of the system.
After the child process completes the task, if the parent process does not check the status in time, the child process will become a zombie process. The child process is waiting for confirmation from the parent process, and the system will not recycle it until it is completed. Otherwise, the zombie process will continue to hang in the system.
To check whether there are zombie processes in the system, you can run the command top to view all running processes and possible zombie processes.
Results of ‘top’ command
From the picture above, you can see the PID number of the process in Linux, and you can also see in the upper right corner that there are no zombie processes in the system.
Are zombie processes harmful to the system?
The zombie process itself will not cause harm to the Linux system, but if there are too many zombie processes, it may cause some minor problems.
A zombie process is a process that has completed its task and is waiting for its parent process to process. If there are too many zombie processes, it may cause system problems.
When zombie processes accumulate too much, system performance may decrease. Therefore, it is very important to check and solve these problems in time. Normally, the responsibility for cleaning up zombie processes lies with their parent processes. If the parent process does not handle zombie processes correctly, it may result in a waste of system resources. Therefore, promptly checking and adjusting the processing method of the parent process can effectively avoid the negative impact of zombie processes on the system.
To eliminate zombie processes, we need to learn some commands to help us identify these processes.
The first command to check is ps. The ps command displays the active processes running in Linux.
However, if you just run the ps command, it won't display much useful information. Therefore, some more flags need to be added to get the information we want.
ps aux
a: Display the processes of all users. u: Displays the user/owner of the process. x: Show processes that are not connected to the terminal.
picture
Processes in Linux
As shown in the above results, there are two zombie processes in the system. (Their STAT is shown as Z)
Since our goal is to find zombie processes, we need to filter out those processes with status Z (i.e. zombie processes) instead of displaying all running processes. This can be achieved using the grep command.
ps aux | grep "Z"
This will filter all zombie processes in the system except other processes.
Note: If there are no zombie processes in the system and you want to continue studying the content of this article, you can run the following command to create some:
(sleep 1 & exec /bin/sleep 999) &
When you run the ps aux | grep ‘Z’ command, it will display all processes containing the letter ‘Z’, including the grep command itself. This is because the grep command is also a process, and its task is to find processes containing ‘Z’, so it will also be searched by itself. Therefore, in order to avoid this problem, you need to add another pipe | grep -v grep, so that you can exclude the processes generated by the grep command itself and only display the real zombie processes.
Now to eliminate the zombie process, some complicated operations are required, because the zombie process cannot be killed directly, but its parent process needs to be killed first, and then the zombie process can be killed. This is because zombie processes are created by their parent processes, and zombie processes cannot be recycled by the system until the parent process releases their resources.
First, you need to find the parent process. You can use the following simple command to achieve this:
ps -o ppid= -p [僵尸进程 PID]
(replace zombie process PID with actual PID number)
This will display the parent process PID of the zombie process and then use that PID to kill the parent process.
Killing a process in Linux is easy. Use the kill command to do this:
ps aux | grep 'Z' | grep -v grep
This will show the results of any processes that are zombies. We get their PID numbers and then use the ps -o ppid= -p [zombie process pid] command to find the zombie process's parent process PID so we can kill it.
picture
Find the parent process PID
In the example, there are three zombie processes with PIDs 109, 117 and 119. Here we find the parent process of zombie process 109.
ps -o ppid= -p 109
The result is very simple. In the example, only the PID number is displayed: 108
To kill the process, just use the kill command:
kill 108
At this point, the parent process of zombie process 109 has been killed.
NOTE: Killing the parent process may have side effects on the system or other applications, so should be performed with caution. Normally, killing the parent process should be done as a last resort, and it is best not to consider killing the parent process until you try other solutions.
In the example, we killed the process we created ourselves for testing purposes, so it's okay, but when you actually decide to kill a parent process, you need to understand what you are doing. First, find the parent process, check what it does and what it does, and then make sure you don't break anything by killing it. Finally, you can kill it using the command above.
By reading this article, I hope readers can understand that zombie processes are not that terrible, even though they may cause some problems, especially when zombie processes start to be crowded together; I hope readers have mastered some weapons/commands, such as using ps aux | grep "Z" to find zombie processes and learned a way to kill them and their parent processes without destroying the system.
The above is the detailed content of How to properly kill zombie processes in Linux. For more information, please follow other related articles on the PHP Chinese website!