Live data from Hacker News

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

redsymbol.net

11–20 of 159 posts

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

#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

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

#12
post #8

[flagged]

bash scripts have their use cases, many things are shorter and simpler than in Python. But coders should bother to learn how bash works and use shellcheck. Just guessing from how things work in another language typically leads to buggy code. Keeping a daemon always running is not a task for bash. systemd is typically much better at that (although something like exponential backoff in case of failure seem to be tricky)

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

#13
post #7

An annoying thing about bash is that EXIT will also run on SIGINT (^C), which most other shells won't (in my reading it's also not POSIX compliant, although the document is a bit vague). Some might argue this is a feature, but IMHO it's a bug – sometimes you really don't want cleanup to happen so people can inspect the contents of temporary files for debugging. Because trap doesn't pass the signal information to the…

[flagged]

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

#14
I just learned about these through “pair” programming with ChatGPT. It is the quintessential ML-enhanced programming trick: Using some old, robust language feature I’m skilled enough to grok but never had the time to learn about through endless documentation spelunking.

My opinion is that LLM pair programming is most or maybe only beneficial to already skilled programmers. ChatGPT can open the door for you, but it can’t show you where the door is. I needed the experience to ask it for a Bash script that handles exit codes gracefully, which is not a question all junior programmers would be able to ask.

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

#15
I like combining this with a bash implementation of an event API (https://github.com/bashup/events). This makes it easy/idiomatic, for example, to conditionally add cleanup as you go.

Glossing over some complexity, but roughly:

    add_cleanup(){
        event on cleanup "$@"
    }
    
    trap "event emit 'cleanup'" HUP EXIT
    
    start_postgres(){
        add_cleanup stop_postgres
        # actually start pg
    }
    
    start_apache(){
        add_cleanup stop_apache
        # actually start apache
    }
I wrote a little about some other places where I've used it in https://www.t-ravis.com/post/shell/neighborly_shell_with_bas... and https://t-ravis.com/post/nix/avoid_trap_clobbering_in_nix-sh... (though I make the best use of it in my private bootstrap and backup scripts...)

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

#16
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

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

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

#17
@redsymbol your site has a TLS certificate error. On Chrome I get NET::ERR_CERT_COMMON_NAME_INVALID because your certificate is from mobilewebup.com

Otherwise a good article. I use the following code to enable passing the signal name to the trap handler, so that I can kill the Bash process with the correct signal name, which is best practice for Unix signal handling (EXIT would have to be handled specially in `sig_rekill`):

    # Set trap for several signals and pass signal name to trap function.
    # https://stackoverflow.com/a/2183063/207384
    trap_with_arg() {
        func="$1" ; shift
        for sig ; do
            trap "$func $sig" "$sig"
        done
    }
    sig_rekill() {
        # Kill whole process group.
        trap "$1"; kill -"$1" -$$
    }
    # Catch signal and kill whole process group.
    trap_with_arg sig_rekill HUP INT QUIT PIPE TERM

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

#19
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

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.

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

#20
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

You can't trap kill can you? doesn't that just go kill your process without any possibility of intervention or other actions? Also you probably want to handle HUP there too I would think (depending on what the script does)
Post reply on HN