search
HomeComputer TutorialsComputer KnowledgeHow to kill zombie processes in Linux

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

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

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

另一种方法是使用 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

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

2、清理僵尸进程

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

2.1 使用 SIGCHLD 信号

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

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

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

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

kill -s SIGCHLD 103

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

2.2 kill 父过程

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

kill -9 103

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

如果存在大量僵尸进程,或者僵尸进程的父进程是 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
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
Fix the 'This Build of Vanguard Is out of Compliance” Error - MiniToolFix the 'This Build of Vanguard Is out of Compliance” Error - MiniToolApr 15, 2025 am 12:50 AM

You may encounter the “this build of Vanguard is out of compliance” issue when attempting to launch Valorant on Windows 11. Why does the error message appear? How to get rid of the error message? This post from php.cn gives details.

How to Download RTX 4050 Drivers on Windows 10/11?How to Download RTX 4050 Drivers on Windows 10/11?Apr 15, 2025 am 12:49 AM

NVIDIA GeForce RTX 40 series GPU might not be a new thing to you. Compared with other top graphics cards, many of you may pay more attention to mid-end mainstream RTX 4050 for its acceptable price. Read through this guide from php.cn Website to get d

KB2267602 Fails to Install: Here Is How to Fix It!KB2267602 Fails to Install: Here Is How to Fix It!Apr 15, 2025 am 12:48 AM

KB2267602 is a protection or definition update for Windows Defender designed to fix vulnerabilities and threats in Windows. Some users reported that they were unable to install KB2267602. This post from php.cn introduces how to fix the “KB2267602 fai

Two Ways to Reinstall Pre-installed Software in Windows 11Two Ways to Reinstall Pre-installed Software in Windows 11Apr 15, 2025 am 12:47 AM

Do you know how to reinstall pre-installed software in Windows 11 if you need to do this? In this post, we will introduce two easy ways. In addition, if you want to recover files on your Windows computer, you can try php.cn Power Data Recovery.

Fixed: Desktop and File Folders Do Not Refresh AutomaticallyFixed: Desktop and File Folders Do Not Refresh AutomaticallyApr 15, 2025 am 12:46 AM

Windows Desktop or File Explorer or folder will automatically refresh itself when you make some changes to it. However, some Windows 11/10 users report that they encounter the “desktop and file folders do not refresh automatically” issue. This post f

Fixed: There Is a Problem with This Windows Installer PackageFixed: There Is a Problem with This Windows Installer PackageApr 15, 2025 am 12:45 AM

When you try to install a program on Windows 11/10, you may fail to install it and receive an error message - there is a problem with this Windows installer package. This post from php.cn helps you to fix it.

Can Antivirus Scan Encrypted Files? Security vs. PrivacyCan Antivirus Scan Encrypted Files? Security vs. PrivacyApr 15, 2025 am 12:44 AM

When you run antivirus software on your device, you likely expect it to scan all files and folders for viruses. However, one question arises: can antivirus scan encrypted files? This inquiry delves into the realm of security versus privacy. php.cn So

Free Download or Update HDMI Video Drivers on a Windows PCFree Download or Update HDMI Video Drivers on a Windows PCApr 15, 2025 am 12:43 AM

How to download HDMI video drivers on Windows? How to update HDMI drivers to the latest versions. You can find the ways here. In addition, you can try php.cn Power Data Recovery to get your lost and deleted files back if necessary.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software