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

The health-check script itself started hanging, and cron has 30 copies running. What happened and how do you harden it?

AdvancedScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → A check blocked forever, most likely df or stat on a hung NFS mount, or a curl without a timeout, and cron kept starting new copies.

What I'd do →

  1. Find what blocks: ps -eo pid,stat,wchan:30,cmd | grep -E 'health|df' (state D on nfs points at the mount).
  2. Bound every external call: timeout 10 df -P, curl --max-time 5, ssh -o ConnectTimeout=5.
  3. Skip network filesystems in disk checks (df -l or -x nfs -x nfs4 -x cifs) and check them separately with a timeout.
  4. Add a single-instance lock (flock -n) and an overall timeout in the scheduler (TimeoutStartSec= in systemd).
  5. Report a hung check as UNKNOWN or CRITICAL instead of hanging.

Why → A monitoring script that hangs both hides problems and adds load during an incident.

Interview tip → Say "every external call gets a timeout" as a rule you always follow.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts