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

You suspect a memory leak in a service. How do you collect evidence with a script before involving developers?

AdvancedScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → You need a time series of the process's memory to show a monotonic increase, plus restarts.

What I'd do →

  1. Sample every minute from a timer, appending CSV:
    pid=$(systemctl show -p MainPID --value app)
    rss=$(awk '/^VmRSS:/ {print $2}' "/proc/$pid/status")
    cg=$(cat "/sys/fs/cgroup/system.slice/app.service/memory.current")
    printf '%s,%s,%s,%s\n' "$(date -u +%FT%TZ)" "$pid" "$rss" "$cg" >> /var/lib/memwatch/app.csv
    
  2. Track the cgroup's memory.current too: it includes child processes and page cache charged to the service.
  3. Summarize growth per hour with awk, and correlate with PID changes (restarts) and deploy times.
  4. Alert if growth exceeds a slope (for example 50 MB per hour sustained for 6 hours).

Why → A graph of steady growth across restarts is the evidence developers need; a single free -m snapshot is not.

Interview tip → Mention that MemoryMax= in the unit turns a leak into a contained restart instead of a host OOM.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts