Live data from Hacker News

“Exit traps” can make your Bash scripts more robust and reliable (2013)

redsymbol.net

51–60 of 159 posts

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#51
post #19

Earlier quoted context omitted.

Harsh lesson, those five signal names are identical if one squints real good. Would have never known.

`man 7 signal` on linux or just `man signal` on macOS will give you more information about the different signals, and shows what the different meanings of those are.

"kill -l" gives you a terse (but complete) list.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#52
Should go without saying, but don't rely on this for anything critical. It's not guaranteed this will run, even on successful completion of the script. Simple example: power is cut between the last line of the script and before the trap runs. Just a heads up

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#54
Don't use traps unless you have to. They are subtly complex and require a great deal more code to deal with edge cases. There is almost always a simpler way to accomplish what you want.

If Bash has taught me anything, it's that many advanced features should seldom be used. Always resist the temptation to be fancy.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#55

Don't use traps unless you have to. They are subtly complex and require a great deal more code to deal with edge cases. There is almost always a simpler way to accomplish what you want. If Bash has taught me anything, it's that many advanced features should seldom be used. Always resist the temptation to be fancy.

How have exit traps come back to bite you?

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#56
post #11

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

The signals EXIT HUP INT TERM cover everything I've run into (I'm actually using EXIT SIGHUP SIGINT SIGTERM but presumably it's equivalent). In basic terms for my purposes these respectively account for a clean exit, the terminal emulator being closed, ctrl-c, the kill command (edit: the default SIGTERM kill -15, not the SIGKILL kill -9)

What's the signal for SIGKILL?

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#57

Don't use traps unless you have to. They are subtly complex and require a great deal more code to deal with edge cases. There is almost always a simpler way to accomplish what you want. If Bash has taught me anything, it's that many advanced features should seldom be used. Always resist the temptation to be fancy.

How have exit traps come back to bite you?

The first is that the trap can come at any time. You can't assume at what point in the script it was running, so you have to test for different cases to find out what you now can/should do. Forget an edge case and now you've got an extra bug. Not using traps, it's clearer what happens at specific points in the execution of the rest of the code, so simpler to reason about how to deal with those cases as/where they happen.

The second is different events can trigger an exit trap, and those may have different implications on what's going on.

The third is there's parts of standards left out about what happens during/after a trap or when they get called, what data you have available, and different implementations can behave differently.

Fourth is that sometimes people will use an exit trap to, say, report on a failure, but they may have lost context of what block they were in when it exited, and now the error reporting doesn't tell you everything you wanted to know.

I can't remember more specifics atm because I stopped using them like a decade ago. I'll still use them to clean up temp files, but I also have to add the cleanup logic to the start of the script in case it didn't run.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#58

Earlier quoted context omitted.

The signals EXIT HUP INT TERM cover everything I've run into (I'm actually using EXIT SIGHUP SIGINT SIGTERM but presumably it's equivalent). In basic terms for my purposes these respectively account for a clean exit, the terminal emulator being closed, ctrl-c, the kill command (edit: the default SIGTERM kill -15, not the SIGKILL kill -9)

What's the signal for SIGKILL?

SIGKILL can't be handled. It's the signal you send when you don't want to give the process a chance to handle it.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#59

Should go without saying, but don't rely on this for anything critical. It's not guaranteed this will run, even on successful completion of the script. Simple example: power is cut between the last line of the script and before the trap runs. Just a heads up

If you were building a critical system, what would do if power is cut after the last line of a script runs?

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#60

>The secret sauce is a pseudo-signal provided by bash, called EXIT, that you can trap; commands or functions trapped on it will execute when the script exits for any reason. "Secret Sauce", why is this secret at all. Nothing against the author who's helping the ecosystem here, but is there an authoritative guide on Bash that anyone can recommend? Hopefully something that's portable between Mac & Linux. The web is ful…

I don't know if it checks the right box as authoritative, but my goto guide has been tldp:

https://tldp.org/LDP/abs/html/

Post reply on HN