title: bash trap of TERM - what am I doing wrong?
body: <p>Given this hack.c program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  main()
  {
   int i=0;
   for(i=0; i&lt;100; i++) {
     printf("%d\n", i);
     sleep(5);
   }
  }
  </code></pre>
  
  <p>and this hack.sh bash script:</p>
  
  <pre><code>#!/bin/bash
  ./hack
  </code></pre>
  
  <p>If I run hack.sh, two processes get created - one for bash, one for the C task.  If a TERM signal gets sent to the bash process, the C process is unharmed.</p>
  
  <p>Now, suppose the original bash was launched from a Java program using Runtime.exec(), so the only control I have over it is Process.destroy() (which sends TERM to the bash process)?  Suppose I <strong>want</strong> the C process to die along with the bash that launched it?</p>
  
  <p>I've been trying things like this in bash:</p>
  
  <pre><code>#!/bin/bash
  trap "kill -TERM -$$; exit" TERM
  ./hack
  </code></pre>
  
  <p>i.e. a trap clause that catches the TERM signal and rebroadcasts it to the whole process group.  This doesn't work for me - a bash process with that trap clause in it <strong>ignores</strong> TERM signals.</p>
  
  <p>What am I missing here?</p>