<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Valentin's Lab &#187; bash signals</title>
	<atom:link href="https://vaab.blog.kal.fr/tag/bash-signals/feed/" rel="self" type="application/rss+xml" />
	<link>https://vaab.blog.kal.fr</link>
	<description>Ratiocination of an opensource techie</description>
	<lastBuildDate>Thu, 15 Nov 2018 08:04:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F&amp;language=en_US&amp;category=text&amp;title=Valentin%27s+Lab&amp;description=Ratiocination+of+an+opensource+techie&amp;tags=blog" type="text/html" />
	<item>
		<title>trapping SIGINT, SIGQUIT in asynchronous tasks</title>
		<link>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/</link>
		<comments>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/#comments</comments>
		<pubDate>Sat, 30 Jul 2016 10:36:31 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[bash signals]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=562</guid>
		<description><![CDATA[Scattered in bash documentation lies the truth about SIGINT and SIGQUIT signals being mysteriously ignored and non-trappable in background processes. This has puzzled many, and will probably continue to do so. Traps Here's my try to unveil the terrible truth. &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Scattered in bash documentation lies the truth about <tt class="docutils literal">SIGINT</tt>
and <tt class="docutils literal">SIGQUIT</tt> signals being mysteriously ignored and
non-trappable in background processes.</p>
<p>This has puzzled many, and will probably continue to do so.</p>
<div class="section" id="traps">
<h3>Traps</h3>
<p>Here's my try to unveil the terrible truth.</p>
<p>Let this script separate all cases and enlighten us:</p>
<pre class="literal-block">
cat &lt;&lt;'EOF' &gt; /tmp/test.sh
#!/bin/bash
traps_show_and_test() {
    local label=&quot;$1&quot;
    echo; echo &quot;TRAPS from $label:&quot;; trap
    trap &quot;echo modified by $label&quot; SIGINT  ## &lt;-- MODIFICATION
    echo &quot;TRAPS from $label (after modification attempt):&quot;; trap
}
export -f traps_show_and_test

traps_show_and_test main
{ traps_show_and_test subshell; }
( traps_show_and_test subparen )
bash -c 'traps_show_and_test bash_subprocess'

traps_show_and_test job &amp; wait
{ bash -c 'traps_show_and_test subshell_job'; } &amp; wait
( bash -c 'traps_show_and_test subparen_job' ) &amp; wait
bash -c 'traps_show_and_test bash_job' &amp; wait

echo
echo FINAL main traps:
trap

EOF
chmod +x /tmp/test.sh
/tmp/test.sh
</pre>
<p>Running the previous script in bash (version is
<tt class="docutils literal"><span class="pre">4.3.46(1)-release</span></tt>) gives the following:</p>
<pre class="literal-block">
TRAPS from main:
TRAPS from main (after modification attempt):
trap -- 'echo modified by main' SIGINT

TRAPS from subshell:
trap -- 'echo modified by main' SIGINT
TRAPS from subshell (after modification attempt):
trap -- 'echo modified by subshell' SIGINT

TRAPS from subparen:
trap -- 'echo modified by subshell' SIGINT
TRAPS from subparen (after modification attempt):
trap -- 'echo modified by subparen' SIGINT

TRAPS from bash_subprocess:
TRAPS from bash_subprocess (after modification attempt):
trap -- 'echo modified by bash_subprocess' SIGINT

TRAPS from job:
trap -- 'echo modified by subshell' SIGINT
TRAPS from job (after modification attempt):
trap -- 'echo modified by job' SIGINT

TRAPS from subshell_job:
TRAPS from subshell_job (after modification attempt):
trap -- 'echo modified by subshell_job' SIGINT

TRAPS from subparen_job:
TRAPS from subparen_job (after modification attempt):
trap -- 'echo modified by subparen_job' SIGINT

TRAPS from bash_job:
trap -- '' SIGINT
trap -- '' SIGQUIT
TRAPS from bash_job (after modification attempt):
trap -- '' SIGINT
trap -- '' SIGQUIT

FINAL main traps:
trap -- 'echo modified by subshell' SIGINT
</pre>
<p>Let's highlight important points:</p>
<ul class="simple">
<li>Foreground Jobs<ul>
<li>subshells from <tt class="docutils literal"><span class="pre">{..}</span></tt> and <tt class="docutils literal"><span class="pre">(..)</span></tt> inherit their traps from the main scope.</li>
<li>only <tt class="docutils literal"><span class="pre">{..}</span></tt> can modify the main scope.</li>
<li>subprocesses do not inherit its trap from its parent process</li>
<li>they all can modify at least locally their traps</li>
</ul>
</li>
<li>Background Jobs<ul>
<li>bash function inherits its traps from main scope but can't modify it (like <tt class="docutils literal"><span class="pre">(..)</span></tt>)</li>
<li>processes in <tt class="docutils literal"><span class="pre">{..}</span> &amp;</tt> and <tt class="docutils literal"><span class="pre">(..)</span> &amp;</tt> do not inherit traps</li>
<li>direct background subprocess have default signed traps that are umodifiable</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="bash-documentation">
<h3>Bash documentation</h3>
<p>As a reference, here are the documentation from bash related to these behaviors:</p>
<p>Process group id effect on background process (in <em>Job Control</em> section of doc):</p>
<blockquote>
[...] processes whose process group ID is equal to the current terminal
process group ID [..] receive keyboard-generated signals such as
SIGINT.  These processes are said  to  be  in the  foreground.
<strong>Background processes</strong> are those whose process group ID  differs from
the terminal's; such processes <strong>are immune to keyboard-generated
signals</strong>.</blockquote>
<p>Default handler for <tt class="docutils literal">SIGINT</tt> and <tt class="docutils literal">SIGQUIT</tt> (in <em>Signals</em> section of doc):</p>
<blockquote>
Non-builtin commands run by bash have signal handlers set to
the values inherited by the shell from its parent.  When job
control is not in effect, <strong>asynchronous commands ignore SIGINT
and SIGQUIT</strong> in addition to these inherited handlers.</blockquote>
<p>and about modification of traps (in <tt class="docutils literal">trap</tt> builtin doc):</p>
<blockquote>
<strong>Signals  ignored upon entry to the shell cannot be trapped or reset</strong>.</blockquote>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=562&amp;md5=880d404889c09219cc780ffed2083da5" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2016%2F07%2F30%2Ftrapping-sigint-sigquit-in-asynchronous-tasks%2F&amp;language=en_GB&amp;category=text&amp;title=trapping+SIGINT%2C+SIGQUIT+in+asynchronous+tasks&amp;description=Scattered+in+bash+documentation+lies+the+truth+about+SIGINT+and+SIGQUIT+signals+being+mysteriously+ignored+and+non-trappable+in+background+processes.+This+has+puzzled+many%2C+and+will+probably+continue+to+do...&amp;tags=bash+signals%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
