A dashboard reads a JSON status file that your script writes every minute. Sometimes the dashboard shows parse errors. Fix it.
IntermediateScenarioScenarios: health checks, monitoring and alerts
Answer
Situation read → The dashboard reads the file while the script is in the middle of writing it.
What I'd do →
out=/var/lib/opskit/status.json
tmp=$(mktemp "${out}.XXXXXX")
jq -n --arg host "$(hostname -s)" --argjson disk "$disk_pct" --arg ts "$(date -u +%FT%TZ)" \
'{host: $host, disk_pct: $disk, updated: $ts}' > "$tmp"
chmod 0644 "$tmp"
mv -f -- "$tmp" "$out"
- Write to a temp file in the same directory, then rename.
- Build JSON with
jqso it is always valid.
Why → Rename is atomic on the same filesystem; readers see the old or new file, never a partial one.
Interview tip → Explain that mktemp creates files with mode 0600, so set permissions before the mv if others must read it.
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?IntermediateYou suspect a memory leak in a service. How do you collect evidence with a script before involving developers?Advanced