症状
ps 列出处于 defunct 状态的进程。它们无法被 kill,因为进程实际已终止;只有其父进程通过回收才能移除条目。
诊断
查找僵尸及其父进程:
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /Z/'
PPID 指向未调用 wait 的进程。
修复
在父进程中回收子进程:
signal(SIGCHLD, SIG_IGN); // auto-reap, platform-dependent behaviour
// or explicitly
while (waitpid(-1, NULL, WNOHANG) > 0) { }
在容器中加入最小 init:
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "server.js"]
验证
持续观察进程表;子进程终止后不再出现 defunct 条目,确认父进程已能回收,PID 槽位得以释放。