HN Hall of Fame Weekly email

“Exit Traps” Can Make Your Bash Scripts Way More Robust and Reliable

redsymbol.net Books & learning Tutorials & guides Software engineering Candidate
Screenshot of redsymbol.net captured 2026-07-20
Page preview · captured 2026-07-20

Resurfaced independently across 4 calendar years, with breakout response in 2 of them.

submissions
4
submitters
4
observed span
2012–2023
peak thread · 148 comments
591 pts
latest 20+ return · 2023-06-20
591 pts

Submission timeline

2007–2026

One slot for every year since HN launched. Height is that year's peak points; orange marks a 100+ point or 50+ comment breakout. Select a bar to open its strongest thread.

First comments on top threads

HN comment order

I used an exit trap to kill an SSH agent that I am running, and I noticed that dash did not kill if the script was interrupted, but only if it ran to successful completion. I asked on the mailing list if this was expected behavior, and it turns out that POSIX only requires EXIT to run on a clean shutdown; to catch interruptions, add more signals. trap 'eval $(ssh-agent -k)' EXIT INT ABRT KILL TERM

chasil·591-point thread·

Traps are neat but beware, they aren't completely reliable. You can't trap SIGKILL or SIGSTOP. In the article's examples, a SIGKILL would (1) leave temp directories around, (2) fail to restart a service, and (3) leave an expensive AWS resource running. Remember that SIGKILL isn't always the result of a human typing `kill -9` either: the Linux OOM killer sends it; all unixes potentially send it during shutdown and runlevel switching; programs like timeout(1) send it as a last resort…

sigil·158-point thread·

The first top-level comment from each of the four biggest threads, in HN’s own order. Excerpts are shortened; open a comment for full context.

Breakout years
2

100+ points or 50+ comments

Total points
761

reference only — not used in Hall rules or ranking

Total comments
186

reference only — not used in Hall rules or ranking

Every submission

DateTitle as submittedByPointsComments
2012-12-28Using Exit Traps In Bashredsymbol100
2016-04-18How “Exit Traps” Can Make Your Bash Scripts Way More Robust and Reliableshocks20
2018-01-13How “Exit Traps” Can Make Bash Scripts More Robust and ReliableFirst breakoutstriking15838
2023-06-20“Exit Traps” Can Make Your Bash Scripts Way More Robust and ReliableBest thread · Latest 20+ point returnekiauhce591148