在 Debian(以及大多数 Linux 发行版)中,僵尸进程(Zombie Process) 是指已经结束执行,但还没有被父进程回收(wait)的进程。它们不占用 CPU 或内存,但会在进程表中保留记录。
下面是几种查看和排查 Debian 僵尸进程的常用方法。
toptop
在顶部信息中如果看到类似:
Tasks: 123 total, 1 running, 121 sleeping, 0 stopped, **1 zombie**
说明当前存在僵尸进程。
ps 查看僵尸进程(推荐)ps aux | grep Z
或更直观一点:
ps -eo pid,ppid,stat,cmd | grep Z
输出示例:
1234 1000 Z [myapp] <defunct>
字段说明:
Z:僵尸状态<defunct>:已失效进程PPID:父进程 ID(很关键)ps 只看僵尸进程ps -eo pid,ppid,state,cmd | awk '$3 == "Z"'
僵尸进程本身无法被 kill,需要找它的父进程:
ps -o pid,ppid,stat,cmd -p <僵尸PID>
或直接:
cat /proc/<僵尸PID>/status | grep PPid
常见原因:
wait() / waitpid()⚠️ 注意:僵尸进程不能直接 kill
kill -HUP <父进程PID>
或:
systemctl restart 服务名
kill -9 <父进程PID>
父进程退出后,僵尸进程会被 init(PID 1)接管并自动回收。
ps -eo pid,stat | grep Z
如果没有输出,说明已清理干净。
wait()signal(SIGCHLD, SIG_IGN)(适用于简单程序)systemd 管理长期运行进程如果你愿意,可以把你的:
ps -eo pid,ppid,stat,cmd | grep Z 输出贴出来,我可以帮你具体分析是哪个程序在制造僵尸进程。