<story><title>“Exit traps” can make your Bash scripts more robust and reliable (2013)</title><url>http://redsymbol.net/articles/bash-exit-traps/</url></story><parent_chain><item><author>chasil</author><text>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.&lt;p&gt;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.&lt;p&gt;&lt;pre&gt;&lt;code&gt;  trap &amp;#x27;eval $(ssh-agent -k)&amp;#x27; EXIT INT ABRT KILL TERM&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>chasil</author><text>I found my original submission to the email list.&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;www.spinics.net&amp;#x2F;lists&amp;#x2F;dash&amp;#x2F;msg02208.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;https:&amp;#x2F;&amp;#x2F;www.spinics.net&amp;#x2F;lists&amp;#x2F;dash&amp;#x2F;msg02208.html&lt;/a&gt;&lt;p&gt;&amp;quot;Signal terminations are not caught by EXIT. It only catches normal
exits. Unfortunately, the EXIT condition is not well-defined by POSIX, so it&amp;#x27;s left to interpretation.&amp;quot;&lt;p&gt;...&lt;p&gt;POSIX is making it unspecified what happens here:&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;austingroupbugs.net&amp;#x2F;view.php?id=621&quot; rel=&quot;nofollow noreferrer&quot;&gt;https:&amp;#x2F;&amp;#x2F;austingroupbugs.net&amp;#x2F;view.php?id=621&lt;/a&gt;&lt;p&gt;The EXIT condition shall occur when the shell terminates normally (exits), and may occur when the shell terminates abnormally as a
result of delivery of a signal (other than SIGKILL) whose trap
action is the default.</text></comment>
<story><title>“Exit traps” can make your Bash scripts more robust and reliable (2013)</title><url>http://redsymbol.net/articles/bash-exit-traps/</url></story><parent_chain><item><author>chasil</author><text>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.&lt;p&gt;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.&lt;p&gt;&lt;pre&gt;&lt;code&gt;  trap &amp;#x27;eval $(ssh-agent -k)&amp;#x27; EXIT INT ABRT KILL TERM&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>lgsymons</author><text>The signals EXIT HUP INT TERM cover everything I&amp;#x27;ve run into (I&amp;#x27;m actually using EXIT SIGHUP SIGINT SIGTERM but presumably it&amp;#x27;s equivalent).&lt;p&gt;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)</text></comment>