Translated 20211015 How...

This commit is contained in:
patrick.zeng 2021-10-22 10:42:59 +08:00
parent 6a88e74c5a
commit a2ccbcdb5f
2 changed files with 117 additions and 127 deletions

View File

@ -1,127 +0,0 @@
[#]: subject: "How to Find and Kill Zombie Process in Linux"
[#]: via: "https://itsfoss.com/kill-zombie-process-linux/"
[#]: author: "Marco Carmona https://itsfoss.com/author/marco/"
[#]: collector: "lujun9972"
[#]: translator: "zengyi1001"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to Find and Kill Zombie Process in Linux
======
_**Brief: This is a quick tip on finding zombie processes in Linux and then killing them. You also learn a thing or two about processes and zombie processes.**_
Before you learn about Zombie process, let me recall what is a process in Linux.
In a few words, a [process][1] is a running instance of a program in performance. It can be foreground (interactive process) or background (not interactive or automatic process). It can be a parent (creator of other processes during run-time) or child (process created by others) process.
In Linux, except for the first init (or systemd) process with PID 0, every other process has a parent process. Processes also have their own child processes.
Dont believe me? Use the `pstree` command in terminal to look at the process tree to see the family tree of your systems processes.
### What is a Zombie process in Linux?
When a child process dies, the parent process is informed so that it can do some clean up like freeing up memory etc. However, child process goes into zombie state if the parent process is not aware of its death. For the parent, the child still exists but the child process is actually dead. This is how zombie processes (also known as defunct processes) are created and stay in the system.
Heres an excellent funny take on the zombie process by Turnoff.us:
![Image credit: Turnoff.us][2]
### Do you really need to worry about Zombie processes?
Here is important to say that zombie processes are not as dangerous as its name can sound.
The problem may arise if your system has limited RAM or if there are too many zombie processes eating up RAM. Also, most Linux processes can have maximum PID set to 32768. If there are no available IDs for other productive tasks, your system may crash.
This rarely happens, but its a possibility, specially if a poorly coded program starts inducing numerous zombie processes.
In such case, it would be a good idea to find and kill zombie process.
### How to find zombie processes?
A process in Linux can have one of the following states:
* D = uninterruptible sleep
* I = idle
* R = running
* S = sleeping
* T = stopped by job control signal
* t = stopped by debugger during trace
* Z = zombie
But where can you see the processes and their respective status? One easy way is to use the terminal and the [top command][3].
![Top command show processes and their status][4]
As you can see in the screenshot above, there are 250 total tasks (or processes), 1 is running, 248 processes are sleeping and 1 is in zombie state.
Now, the question arises, how to kill the zombie process?
### How to find and kill a zombie process? Can a zombie process be killed?
![][5]
A zombie process is already dead. How do you kill an already dead process?
In the zombie movies, you shoot the zombies in the head or burn it. Thats not an option here. You can burn your system for killing the zombie process but thats not a feasible solution ;)
Some people suggests sending SIGCHLD signal to the parent process. But it is more likely to be ignored. The other option to kill the zombie process is to kill its parent process. That sounds brutal but thats the only sure shot way of killing zombie processes.
So, first, lets list the zombie processes to know their ID. It can be achieved by [using the ps command][6] like this in the terminal.
```
ps ux | awk '{if($8=="Z+") print}'
```
The 8th column in the output of the ps ux command displays the state of a process. You are asking to print all the matching lines where the state of a process is Z+ (indicating zombie state).
Once you have identified its process ID, lets get its parents process ID.
```
ps -o ppid= -p <child_id>
```
Alternatively, you can combine the above two commands in the following fashion where it directly provides the PID of the zombie process and the PID of its parent process.
```
ps -A -ostat,pid,ppid | grep -e '[zZ]'
```
Here you get the parent process ID, so [finally kill the process][7] by typing the command line with its respective ID process obtained before.
```
kill -9 <parent_process_ID>
```
![Killing parent process][8]
You can verify if the zombie process is killed or not by running the ps command again or even the top command.
Congrats! Now you know how to eliminate zombie processes.
_With inputs from Abhishek Prakash._
--------------------------------------------------------------------------------
via: https://itsfoss.com/kill-zombie-process-linux/
作者:[Marco Carmona][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/marco/
[b]: https://github.com/lujun9972
[1]: https://tldp.org/LDP/tlk/kernel/processes.html
[2]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff.webp?resize=800%2C467&ssl=1
[3]: https://linuxhandbook.com/top-command/
[4]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/10/top-command-view.png?resize=800%2C474&ssl=1
[5]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/10/kill-zombie-process-linux.jpg?resize=800%2C450&ssl=1
[6]: https://linuxhandbook.com/ps-command/
[7]: https://itsfoss.com/how-to-find-the-process-id-of-a-program-and-kill-it-quick-tip/
[8]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/10/killing-parent-process.png?resize=800%2C180&ssl=1

View File

@ -0,0 +1,117 @@
[#]: subject: "How to Find and Kill Zombie Process in Linux"
[#]: via: "https://itsfoss.com/kill-zombie-process-linux/"
[#]: author: "Marco Carmona https://itsfoss.com/author/marco/"
[#]: collector: "lujun9972"
[#]: translator: "zengyi1001"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
如何找到并杀掉 Linux 系统中的僵尸进程
======
_**概述: 这是一个关于如何寻找 Linux 系统僵尸进程并杀死它们的小贴士。你也可以从中了解到关于进程和僵尸进程的一些知识。**_
在了解僵尸进程之前,让我们来复习一下什么是 Linux 进程
简而言之,[进程][1]是一个程序的运行实体。它可能运行在前端(比如有交互的进程),也可能运行在后端(比如无交互或自动运行的进程)。它可能是一个父进程(运行期间创建了其他进程),也可能是一个子进程(由其他进程所创建)。
在 Linux 系统中,除 PID 为 0 的第一个初始进程(或系统进程)外,其余进程都有父进程。进程也可以拥有自己的子进程。
不相信?可以试试在终端中使用 `pstree` 命令查看进程的树型结构,你能看到系统各个进程的 '家族树'。
### Linux系统里的僵尸进程是什么
子进程死亡后,它的父进程会接收到通知去执行一些清理操作,如释放内存之类。然而,若父进程并未察觉到子进程死亡,子进程就会进入到“僵尸”状态。从父进程角度看,子进程仍然存在,即使子进程实际上已经死亡。这就是“僵尸进程”(也被称为“已消失进程“)是如何产生并存在于系统中的。
这里有一个来自 Turnoff.us 的关于僵尸进程的非常有趣的看法:
![Image credit: Turnoff.us][2]
### 你真的需要关心僵尸进程吗?
重点要说的是,僵尸进程并没有像它的名称那样看起来可怕。
但如果系统的 RAM 已经所剩不多或者有太多的僵尸进程在吃掉 RAM问题会变得糟糕。同样大部分 Linux 系统进程最大 PID 设置为 32768如果过多僵尸进程导致其他重要任务没有 PID 可用,你的系统会发生崩溃。
这是真实可能发生的,它有一定的概率,特别当存在一个编码糟糕的程序开始大量产生僵尸进程的时候。
在这种情况下,找到并杀死僵尸进程是一个明智的做法。
### 如何找到僵尸进程
Linux 系统中的进程可能处于如下状态中的一种:
* D = 不可中断
* I = 空闲
* R = 运行中
* S = 休眠
* T = 被调度信号终止
* t = 被调试器终止
* Z = 僵尸状态
那如何查看进程和它的当前状态呢?一个简单的方法是在终端中使用 [top 命令][3].
![Top command show processes and their status][4]
正如你在上面截图中看到的,截图中共有 250 个任务(进程),其中 1 个处在 ”运行中“ 状态248 个进程处于 “休眠” 状态,还有一个处于 “僵尸” 状态。
现在问题进入下一步,如何杀死 ”僵尸” 进程?
### 如何找到并杀死一个僵尸进程?僵尸进程能被杀死吗?
![][5]
僵尸进程已经死了,要如何才能杀死一个已经死亡的进程呢?
在僵尸电影中,你可以射击僵尸的头部或烧掉它们,但在这里是行不通的。你可以一把火烧了系统来杀死僵尸进程,但这并不是一个可行的方案 ;)
一些人建议发送 SIGCHLD 给父进程,但这个信号很可能会被忽略。还有一个方法是杀死父进程来杀死僵尸进程,这听起来很野蛮,但它却是唯一能确保杀死僵尸进程的方法。
首先,通过在终端中 [使用 ps 命令][6] 我们列举僵尸进程,得到它们的进程 ID。
```
ps ux | awk '{if($8=="Z+") print}'
```
ps ux 命令输出的第 8 列显示了进程状态。上述命令只会打印所有处在 Z+ 状态(表示僵尸状态)的进程。
确认了进程 ID 后,我们可以得到它的父进程 ID。
```
ps -o ppid= -p <child_id>
```
你也可以将上述两个命令结合在一起,直接得到僵尸进程的 PID 及其父进程的 PID。
```
ps -A -ostat,pid,ppid | grep -e '[zZ]'
```
现在你得到了父进程 ID使用命令行和得到的 ID 号[终于可以杀死进程了][7]。
```
kill -9 <parent_process_ID>
```
![Killing parent process][8]
再次运行 ps 命令或 top 命令,你可以验证僵尸进程是否已经被杀死。
恭喜!现在你知道怎么清理僵尸进程了。
Abhishek Prakash参与写作。
--------------------------------------------------------------------------------
via: https://itsfoss.com/kill-zombie-process-linux/
作者:[Marco Carmona][a]
选题:[lujun9972][b]
译者:[zengyi1001](https://github.com/zengyi1001)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/marco/
[b]: https://github.com/lujun9972
[1]: https://tldp.org/LDP/tlk/kernel/processes.html
[2]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/10/zombies-turnoff.webp?resize=800%2C467&ssl=1
[3]: https://linuxhandbook.com/top-command/
[4]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/10/top-command-view.png?resize=800%2C474&ssl=1
[5]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/10/kill-zombie-process-linux.jpg?resize=800%2C450&ssl=1
[6]: https://linuxhandbook.com/ps-command/
[7]: https://itsfoss.com/how-to-find-the-process-id-of-a-program-and-kill-it-quick-tip/
[8]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/10/killing-parent-process.png?resize=800%2C180&ssl=1