title: Making sure a Python script with subprocesses dies on SIGINT
body: <p>I've got a command that I'm wrapping in <code>script</code> and spawning from a Python script using <code>subprocess.Popen</code>. I'm trying to make sure it dies if the user issues a <code>SIGINT</code>.</p>
  
  <p>I could figure out if the process was interrupted in a least two ways:</p>
  
  <p>A. Die if the wrapped command has a non-zero exit status (doesn't work, because <code>script</code> seems to always return 0)</p>
  
  <p>B. Do something special with <code>SIGINT</code> in the parent Python script rather than simply interrupting the subprocess. I've tried the following:</p>
  
  <pre><code>import sys
  import signal
  import subprocess
  
  def interrupt_handler(signum, frame):
      print "While there is a 'script' subprocess alive, this handler won't executes"
      sys.exit(1)
  signal.signal(signal.SIGINT, interrupt_handler)
  
  for n in range( 10 ):
      print "Going to sleep for 2 second...Ctrl-C to exit the sleep cycles"
  
      # exit 1 if we make it to the end of our sleep
      cmd = [ 'script', '-q', '-c', "sleep 2 &amp;&amp; (exit 1)", '/dev/null']
      p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  
      while True:
          if p.poll() != None :
              break
          else :
              pass
  
      # Exiting on non-zero exit status would suffice
      print "Exit status (script always exits zero, despite what happened to the wrapped command):", p.returncode
  </code></pre>
  
  <p>I'd like hitting Ctrl-C to exit the python script. What's happening instead is the subprocess dies and the script continues.</p>