Alert when any systemd unit on a host is in a failed state.
BeginnerScenarioScenarios: health checks, monitoring and alerts
Answer
Situation read → systemd already tracks failed units; just query it.
What I'd do →
mapfile -t failed < <(systemctl list-units --state=failed --no-legend --plain | awk '{print $1}')
if (( ${#failed[@]} )); then
echo "CRIT failed units: ${failed[*]}"
for u in "${failed[@]}"; do journalctl -u "$u" -n 5 --no-pager; done
exit 2
fi
echo "OK no failed units"
systemctl is-system-runningreturnsdegradedwhen any unit failed, a quick one-word check.- After fixing,
systemctl reset-failedclears the state.
Why → Failed oneshot units (backups, cert renewals) are easy to miss because nothing restarts them.
Interview tip → Mention systemctl is-system-running for a single status word.
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