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

A developer wrote a Bash loop that restarts the app whenever its process disappears. What do you suggest instead?

IntermediateScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → A watchdog loop is a fragile supervisor: it can die itself, misses fast crash loops, and has no backoff or logging.

What I'd do →

  1. Run the app as a systemd service with restart policy:
    [Service]
    ExecStart=/srv/app/current/bin/server
    Restart=on-failure
    RestartSec=5
    StartLimitIntervalSec=300
    StartLimitBurst=5
    
  2. Add WatchdogSec= if the app can send sd_notify heartbeats, which catches hangs, not just exits.
  3. Alert via OnFailure= when the start limit is hit.
  4. In Kubernetes, the equivalent is liveness probes and the restart policy.

Why → The init system already supervises processes reliably, with logging and rate limiting.

Interview tip → Say "don't write a supervisor in Bash" and name the systemd directives.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts