Interview Prep/Shell Scripting/Troubleshooting: strict mode, exit codes, pipelines and traps

A health-check wrapper reports `check failed: unknown exit code 124` for `timeout 20 ./check_db.sh`. What does 124 mean, and what else can `timeout` return?

BeginnerTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer

Likely causes → GNU timeout returns 124 when the time limit expired and it sent the signal. The check is hanging (DNS, a DB connection with no client timeout, a lock), not failing.

Diagnose →

timeout 20 ./check_db.sh; echo "rc=$?"
timeout -v 20 ./check_db.sh          # -v prints "timeout: sending signal TERM to command ..."
bash -x ./check_db.sh                # see which step hangs

Codes to know: 124 timed out, 125 timeout itself failed, 126 command not executable, 127 command not found, 137 killed with -s KILL (or by --kill-after).

Fix → Map 124 to a meaningful state and add timeouts inside the check, so it fails fast with a reason:

timeout --kill-after=5 20 ./check_db.sh; rc=$?
case $rc in
  0) state=OK ;; 124|137) state=CRITICAL; reason="check timed out after 20s" ;; *) state=CRITICAL ;;
esac

Inside: psql "connect_timeout=5 ...", curl --max-time 5.

Prevent → Every external call in a check has its own timeout, and the wrapper has an outer timeout --kill-after.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: strict mode, exit codes, pipelines and traps