在 Ubuntu(以及所有 Linux 系统)中,僵尸进程(Zombie Process)不能直接被 kill 掉,因为它们已经“死”了,只是还在进程表里占个位置,等待父进程读取它的退出状态。
唯一正确的清理方式是让它的父进程回收它。
下面按「原理 → 快速定位 → 清理方法 → 预防」来说明。
exit)wait() / waitpid()ps aux | grep Z
或
ps -eo pid,ppid,stat,cmd | grep Z
看到 STAT=Z 就是僵尸进程。
僵尸进程只能由父进程清理。
ps -eo pid,ppid,stat,cmd | grep Z
示例输出:
1001 800 Z [sh] <defunct>
1001:僵尸进程 PID800:父进程 PID ✅kill -HUP 800
或
systemctl restart 父进程服务名
父进程重启后,通常会重新
wait()子进程,僵尸就会消失。
kill -9 800
init(PID 1)接管init 会自动 wait() 并清理⚠️ 不要乱杀系统关键进程
ps -eo pid,ppid,stat,cmd | grep Z
没有输出 = 已清理 ✅
❌ 不能这样清理僵尸进程:
kill -9 僵尸PID
原因:
僵尸进程已经死了,
kill对它无效。
systemd / 服务ps -fp 800
systemctl status 服务名
然后:
systemctl restart 服务名
ps -eo pid,ppid,stat,cmd | grep Z
如果 PPID = 1,说明:
systemd 会自动回收✅ 解决方法:
sudo systemctl daemon-reexec
或重启系统(最后手段)
SIGCHLDwait() / waitpid()SIGCHLD 中回收子进程示例(C):
signal(SIGCHLD, SIG_IGN); // 让系统自动回收
或
while (waitpid(-1, NULL, WNOHANG) > 0);
fork 却不回收supervisor / systemd 管理进程# 查看僵尸进程
ps -eo pid,ppid,stat,cmd | grep Z
# 查看父进程
ps -fp 父PID
# 重启父进程
kill -HUP 父PID
如果你愿意,可以把你当前的:
ps -eo pid,ppid,stat,cmd | grep Z
输出贴出来,我可以帮你精确判断该杀哪个进程、怎么处理最安全。