Interview Prep/Shell Scripting/Scenarios: health checks, monitoring and alerts

Monitoring shows hundreds of zombie processes on a host. How do you detect and report them in a script?

BeginnerScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → Zombies (Z state) are finished children whose parent has not called wait(). They use no CPU or memory, only PID slots, but they point to a buggy parent.

What I'd do →

ps -eo stat=,ppid= | awk '$1 ~ /^Z/ {z[$2]++} END {for (p in z) print p, z[p]}' |
while read -r ppid count; do
  echo "WARN $count zombies under ppid $ppid ($(ps -o comm= -p "$ppid"))"
done
  1. You cannot kill a zombie; fix or restart the parent, after which init reaps them.
  2. In containers, add an init (docker run --init, tini) when the main process does not reap children.

Why → Reporting the parent tells you what to fix; counting zombies alone does not.

Interview tip → Say "kill the parent, not the zombie".

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts