Table of Contents
1.1 进程状态
1.2 僵尸进程是如何产生的?
1.3 搜索僵尸进程
2、清理僵尸进程" >2、清理僵尸进程
2.1 使用 SIGCHLD 信号" >2.1 使用 SIGCHLD 信号
2.2 kill 父过程" >2.2 kill 父过程
Home Computer Tutorials Computer Knowledge How to kill zombie processes in Linux

How to kill zombie processes in Linux

Feb 19, 2024 pm 04:24 PM
linux process lecturer

This article mainly introduces Linux zombie processes and methods to clean up zombie processes.

1. What is a zombie process?

Linux中的僵尸进程(Zombie processes)有时也被称为失效或死亡进程。

它们是已执行完毕的进程,但其条目并未从进程表中删除。

How to kill zombie processes in Linux

1.1 进程状态

Linux 会维护一个进程表,其中包含所有正在运行的进程及其状态。

下面简要介绍一下各种进程状态: 

(1)Running (R): 表示这些进程目前正在运行或可运行,用字母 R 表示。需要说明:进程是 R 状态,不代表正在运行,代表可被调度。换句话说,进程只有是 R 状态才可被调度,其他状态要先转为 R 状态,才能被 OS 调度;

(2)Waiting (S/D): 表示这些进程正在等待事件完成或某种资源就绪,用字母 S/D 表示,等待可以是可中断休眠 S(interruptible sleep) 或不中断休眠 D (uninterruptible sleep);

(3)Stopped (T):  可以通过发送 SIGSTOP 信号给进程来停止(T)进程。这个被暂停的进程可以通过发送 SIGCONT 信号让进程继续运行。

(4)Zombie (Z): 一个比较特殊的状态。当子进程退出并且父进程没有读取到子进程退出的返回代码时,就会产生僵死(尸)进程。僵死进程会以终止状态保持在进程表中,并且会一直在等待父进程读取退出状态代码。所以,只要子进程退出,父进程还在运行,但父进程没有读取子进程状态,子进程进入 Z 状态。

1.2 僵尸进程是如何产生的?

当一个进程完成其工作时,Linux 内核会通过发送 SIGCHLD 信号通知其父进程。然后,父进程执行 wait() 系统调用,读取子进程的状态并读取其退出代码。这会清除进程表中子进程条目,从而结束进程。

但是,如果父进程没有在创建子进程时执行 wait() 系统调用,就不会进行适当的清理。在这种情况下,父进程无法监控子进程的状态变化,最终会忽略 SIGCHLD 信号。这将导致已完成进程的僵尸状态留在进程表中,从而使其作为僵尸进程出现在进程列表中。

另一种情况是,父进程无法处理或接收来自子进程的 SIGCHLD 信号,这种情况也会导致僵尸的产生。

1.3 搜索僵尸进程

使用 ps 命令来检索僵尸进程列表:

ps ux
USER PID %CPU %MEMVSZ RSS TTYSTAT START TIME COMMAND
shubh90.00.0169162760 tty1 SDec19 0:00 /bin/bash --login
shubh1080.00.00 0 tty1 Z16:25 0:00 [zombie] <defunct> 
shubh1090.00.0173841928 tty2 R16:25 0:00 ps ux
Copy after login

从输出中可以看出,STAT 列中的 Z 即为僵尸进程状态,或者使用 awk 命令根据 Z 进程状态进一步过滤输出:

ps ux | awk '{if($8=="Z") print}'
shubh 1080.00.00 0 tty1 Z16:25 0:00 [zombie] <defunct>
Copy after login

另一种方法是使用 top 命令:

top
Tasks: 8 total, 1 running, 6 sleeping, 0 stopped, 1 zombie
%Cpu(s):0.7 us,1.6 sy,0.0 ni, 96.5 id,0.0 wa,1.2 hi,0.0 si,0.0 st
KiB Mem :8269412 total,3161228 free,4878832 used, 229352 buff/cache
KiB Swap: 15483260 total, 14830144 free, 653116 used.3256848 avail Mem

PID USERPRNIVIRTRESSHR S%CPU %MEM TIME+ COMMAND
1 root20 08936192148 S 0.00.0 0:00.17 init
8 root20 08936 96 56 S 0.00.0 0:00.00 init
9 shubh 20 0 16916 2748 2640 S 0.00.0 0:00.43 bash
 76 root20 08936224184 S 0.00.0 0:00.00 init
 77 shubh 20 0 16784 3432 3332 S 0.00.0 0:00.35 bash
161 shubh 20 0 000 Z 0.00.0 0:00.00 zombie
162 shubh 20 0 17624 2084 1508 R 0.00.0 0:00.00 top
Copy after login

top 除了输出其他详细信息外,还可以在输出顶部的摘要中看到僵尸进程的数量。

2、清理僵尸进程

我们无法真正杀死僵尸进程,因为本身它已经结束了。但是,可以使用一些方法来清理僵尸进程。

2.1 使用 SIGCHLD 信号

可以手动向僵尸进程的父进程发送 SIGCHLD 信号。这样,父进程就会主动触发 wait()系统调用,从而从进程表中清除已失效的子进程。

找到僵尸进程的父进程 PID: 

ps -A -ostat,pid,ppid | grep -e '[zZ]'
Z108 103
Copy after login

这里 108 表示僵尸进程 PID,103 表示其父进程 PID,接下来,可以使用 kill 命令向父进程发送 SIGCHLD 信号:

kill -s SIGCHLD 103
Copy after login

不过,并不能保证向父进程发送 SIGCHLD 信号就能杀死僵尸进程。只有在父进程可以处理 SIGCHLD 信号的情况下,它才会起作用。

2.2 kill 父过程

如果上一节的方法无法清除失效进程,就需要考虑杀死其父进程:

kill -9 103
Copy after login

但是,杀死父进程会影响其所有子进程。因此,应该格外谨慎,在杀死父进程之前必须确定其影响。

如果存在大量僵尸进程,或者僵尸进程的父进程是 init 进程(pid=1),可以考虑重启系统来清除失效进程。

The above is the detailed content of How to kill zombie processes in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles