在 Ubuntu(和其他 Linux 系统)中,僵尸进程(Zombie Process)本身无法直接被 kill 掉,因为它们已经结束运行,只是还留在进程表中等父进程“收尸”。下面按原理 → 排查 → 处理 → 预防一步步说明。
Z(Zombie)wait() / waitpid()toptop
如果有僵尸进程,会看到类似:
Tasks: 210 total, 1 running, 208 sleeping, 0 stopped, 1 zombie
ps aux | grep 'Z'
或更直观:
ps -eo pid,ppid,stat,cmd | grep Z
示例输出:
1234 1000 Z [example] <defunct>
说明:
1234:僵尸进程 PID1000:父进程 PIDZ:僵尸状态kill -HUP 父进程PID
或直接重启服务:
sudo systemctl restart 服务名
示例:
sudo systemctl restart nginx
kill -9 父进程PID
init / systemd 接管并回收kill -9 僵尸进程PID
✅ 完全没用
僵尸进程已经死透了,kill 不掉
ps -eo pid,ppid,stat,cmd | grep Z
如果频繁出现,说明:
wait)C / C++
waitpid(-1, NULL, WNOHANG);
Shell
wait
Python
import os
os.waitpid(-1, os.WNOHANG)
systemd 管理服务ps -eo pid,ppid,stat,cmd | grep Z
找到父进程 PID(PPID)
重启父进程
sudo systemctl restart 服务名
kill -9 父进程PID
如果你愿意,可以把你当前的:
ps -eo pid,ppid,stat,cmd | grep Z
输出贴出来,我可以帮你精确定位是哪一个服务或程序在制造僵尸进程。