Interview Prep/Shell Scripting/Scenarios: text processing and scheduling decisions

A scheduled report job sometimes runs for hours and piles up. It must be killed if it exceeds one hour. How?

BeginnerScenarioScenarios: text processing and scheduling decisions
Answer

Situation read → Enforce a hard limit at the scheduler level and handle the timeout clearly.

What I'd do →

  1. systemd: TimeoutStartSec=1h in a Type=oneshot service; systemd sends SIGTERM, then SIGKILL after TimeoutStopSec, and marks the run failed (Failed with result 'timeout').
  2. cron: flock -n /run/lock/report.lock timeout -k 60 1h /opt/jobs/report.sh.
  3. In the script, trap TERM to clean up partial output.
  4. Alert on timeouts separately from errors, and investigate why it got slow.

Why → A hard limit stops runaway jobs from stacking up and exhausting resources.

Interview tip → Mention timeout's exit code 124 so the wrapper can report "timed out" accurately.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: text processing and scheduling decisions