The nightly export was `Killed` (dmesg shows `Out of memory: Killed process 4121 (export.sh)`). The `EXIT` trap never ran, and a half-written `report.csv` was published to the dashboard.
IntermediateTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → The OOM killer sends SIGKILL, which cannot be trapped, so no cleanup runs (exit status 137 if anyone was waiting). The script wrote directly to the final path, so the partial file is visible.
Diagnose →
dmesg -T | grep -i 'killed process'
journalctl -k --since today | grep -i oom
systemctl status opskit-export.service # "status=9/KILL" / "Failed with result 'oom-kill'"
Fix → Write to a temp file in the same filesystem and rename only on success, because mv within a filesystem is atomic:
tmp=$(mktemp "${out}.XXXXXX")
generate_report > "$tmp"
mv -f -- "$tmp" "$out"
Then fix the memory use (stream with awk instead of loading everything into an array) and give the job a limit, for example MemoryMax=1G in the unit, so it fails predictably.
Prevent → Design every script for kill -9: atomic renames, idempotent re-runs, stale temp files cleaned on the next start (find "$dir" -name '*.tmp.*' -mmin +60 -delete).
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