An `ERR` trap runs `send_alert`, but when the alert itself fails (Slack is down), the script loops, printing the same error repeatedly until it is killed.
AdvancedTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → Commands run inside the trap handler can trigger the trap again when they fail (with set -E and functions involved, the handler's own failing curl re-enters it). A handler that retries or re-raises can recurse.
Diagnose → bash -x shows on_err being entered from within on_err. trap -p ERR confirms what is registered.
Fix → Disable the trap at the start of the handler and make the alert best-effort:
on_err() {
local rc=$? cmd=$BASH_COMMAND line=${BASH_LINENO[0]}
trap - ERR
set +e
log ERROR "'$cmd' failed ($rc) at line $line"
send_alert CRITICAL "backup failed at line $line" || log WARN "alert delivery failed"
exit "$rc"
}
trap on_err ERR
Prevent → Trap handlers must never fail: no strict mode inside, short timeouts on network calls (curl --max-time 5), and an explicit exit with the original status.
More on Troubleshooting: strict mode, exit codes, pipelines and traps
A script with `set -e` stops silently right after `((retries++))` on its first pass. No error message, exit code 1.Intermediate`notify.sh: line 9: SLACK_WEBHOOK: unbound variable` — but the webhook is meant to be optional.BeginnerOn a CentOS 7 / Amazon Linux 2 host, `deploy.sh "${extra_args[@]}"` fails with `extra_args[@]: unbound variable`. It works on your laptop.Advanced`local release=$(curl -fsS "$API/latest")` fails with a 503, yet the function happily continues and deploys an empty release name.IntermediateA check `procs=$(ps aux | grep -v grep | grep -c '[n]ginx: worker')` kills the script under `set -euo pipefail` whenever nginx is down, before the alert code runs.IntermediateA script that does `first=$(sort -u huge.txt | head -n 1)` exits with status 141 under `pipefail`, but only on large inputs.Advanced