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

You need to alert if "Out of memory" appears more than 50 times in 5 minutes in an app log, without a logging stack. How?

IntermediateScenarioScenarios: health checks, monitoring and alerts
Answer

Situation read → Count matches in a time window. Using the journal or a byte offset avoids rescanning the whole file.

What I'd do →

  1. If the app logs to journald:
    n=$(journalctl -u app --since '5 min ago' --no-pager -o cat | grep -c 'Out of memory' || true)
    (( n > 50 )) && opskit-alert -s critical -k app-oom -t "app OOM errors" "$n in 5 minutes"
    
  2. For a file, remember the last byte offset in a state file and read only new data with tail -c +$((offset + 1)), resetting if the file shrank (rotation).
  3. Run it every 5 minutes from a timer.

Why → Windowed counts give rate-based alerts without Loki or Elasticsearch.

Interview tip → Mention handling rotation (file shrank or inode changed), which is where homemade log watchers break.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: health checks, monitoring and alerts