process-cleanup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Process Cleanup

进程清理

Detect and reap zombie processes using
/bin/ps
.
使用
/bin/ps
检测并收割僵尸进程。

Background

背景知识

A zombie (state
Z
) is a process that has exited but whose parent hasn't called
wait()
to collect its exit status. Zombies consume no CPU or memory, but they hold a PID slot and can accumulate. You can't
kill
a zombie — it's already dead. The only remedies are:
  1. Signal the parent with
    SIGCHLD
    so it reaps the child
  2. Kill the parent so
    init
    /
    launchd
    adopts and reaps the orphan
僵尸进程(状态为
Z
)是指已经退出但父进程尚未调用
wait()
来收集其退出状态的进程。僵尸进程不消耗CPU或内存,但会占用PID槽位,并且可能会累积。你无法
kill
僵尸进程——它已经死了。唯一的解决方法是:
  1. 向父进程发送
    SIGCHLD
    信号
    ,使其收割子进程
  2. 杀死父进程,让
    init
    /
    launchd
    接管并收割孤儿进程

Important: use
ps
for process state queries

重要提示:使用
ps
进行进程状态查询

Always use
ps
(BSD) for process state queries. If
ps
is aliased, use
/bin/ps
to bypass it.
bash
undefined
始终使用
ps
(BSD版本)进行进程状态查询。如果
ps
被设置了别名,请使用
/bin/ps
来绕过别名。
bash
undefined

List zombies

列出僵尸进程

/bin/ps ax -o pid,state,ppid,user,command | awk '$2 ~ /Z/'
/bin/ps ax -o pid,state,ppid,user,command | awk '$2 ~ /Z/'

List all processes with state

列出所有带状态的进程

/bin/ps ax -o pid,state,ppid,etime,args
undefined
/bin/ps ax -o pid,state,ppid,etime,args
undefined

Usage

使用方法

Run the helper script to scan for zombies:
bash
undefined
运行辅助脚本扫描僵尸进程:
bash
undefined

Detect only (default) — list zombie processes

仅检测(默认模式)——列出僵尸进程

bash scripts/kill-zombies.sh
bash scripts/kill-zombies.sh

Reap zombies — send SIGCHLD to parent processes

收割僵尸进程——向父进程发送SIGCHLD信号

bash scripts/kill-zombies.sh --kill
undefined
bash scripts/kill-zombies.sh --kill
undefined

Safety

安全说明

  • Default mode is detect-only — no processes are signaled
  • With
    --kill
    , the script sends
    SIGCHLD
    to parent processes (non-destructive nudge to reap)
  • If a parent ignores
    SIGCHLD
    , the script reports the parent PID — confirm with the user before killing it
  • Never kill PID 1 (
    init
    /
    launchd
    )
  • 默认模式为仅检测——不会向任何进程发送信号
  • 使用
    --kill
    参数时,脚本会向父进程发送
    SIGCHLD
    信号(这是一种非破坏性的提示,促使其收割僵尸进程)
  • 如果父进程忽略
    SIGCHLD
    信号,脚本会报告父进程的PID——在杀死它之前请先与用户确认
  • 切勿杀死PID为1的进程(
    init
    /
    launchd