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 →
- 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 - Track the cgroup's
memory.currenttoo: it includes child processes and page cache charged to the service. - Summarize growth per hour with
awk, and correlate with PID changes (restarts) and deploy times. - 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.
More on Scenarios: health checks, monitoring and alerts
You are asked for a host health-check script covering disk, memory, systemd services and an HTTP endpoint. How do you design it?IntermediateYour disk alert fires and clears every few minutes around 80%, spamming Slack. How do you fix the alerting?AdvancedA server reports `No space left on device` but `df -h` shows 40% free. What would you add to the health checks?BeginnerAlert when any certificate under `/etc/letsencrypt/live` expires within 21 days.IntermediateA developer wrote a Bash loop that restarts the app whenever its process disappears. What do you suggest instead?IntermediateHow do you know a scheduled job actually ran, not just that it did not report failure?Intermediate