title: trap not working in shell script?
body: <p>I'm in the process of writing a script on OS X that needs to trap SIGTERM in order to kill some child processes before exiting.  For the sake of this question I've boiled it down to the following minimal example that, for some reason, isn't working as I'd expect it to:</p>
  
  <pre><code>#!/bin/sh
  
  function shutdown()
  {
    touch foo.txt
    exit 0
  }
  
  trap shutdown TERM
  su -l myusername -c "sleep 9999"
  </code></pre>
  
  <p>I run this script in one terminal window, then switch to another and "ps -aef" yields this:</p>
  
  <pre><code>  502   857   645   0 11:38PM ttys001    0:00.00 /bin/sh ./foo.sh
      0   858   857   0 11:38PM ttys001    0:00.02 su -l myusername -c sleep 9999
    502   859   858   0 11:38PM ttys001    0:00.00 sleep 9999
  </code></pre>
  
  <p>From that second window I then issue "kill -15 857", but the trap is never triggered.  The script remains blocked on the "su" command.</p>
  
  <p>Any idea why?  I get the feeling it's something simple.</p>