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.
“Exit traps” can make your Bash scripts more robust and reliable (2013)
51–60 of 159 posts
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#52Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#53Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#54If 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)
#55Don'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)
#56I 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)
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#57Don'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 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)
#58Earlier 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?
Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)
#59Should 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)
#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…