title: Handling keyboard interrupt when using subproccess
body: <p>I have python script called <code>monitiq_install.py</code> which calls other scripts (or modules) using the <code>subprocess</code> python module. However, if the user sends a keyboard interrupt (CTRL + C) it exits, but with an exception. I want it to exit, but nicely.</p>
  
  <p>My Code:</p>
  
  <pre><code>import os
  import sys
  from os import listdir
  from os.path import isfile, join
  from subprocess import Popen, PIPE
  import json
  
  # Run a module and capture output and exit code
  def runModule(module):
      try:
          # Run Module
          process = Popen(os.path.dirname(os.path.realpath(__file__)) + "/modules/" + module, shell=True, stdout=PIPE, bufsize=1)
          for line in iter(process.stdout.readline, b''):
              print line,
  
          process.communicate()
          exit_code = process.wait();
  
          return exit_code;
      except KeyboardInterrupt:
          print "Got keyboard interupt!";
          sys.exit(0);
  </code></pre>
  
  <p>The error I'm getting is below:</p>
  
  <pre><code>python monitiq_install.py -a
  Invalid module filename: create_db_user_v0_0_0.pyc
  Not Running Module: '3parssh_install' as it is already installed
  ######################################
  Running Module: 'create_db_user' Version: '0.0.3'
  Choose username for Monitiq DB User [MONITIQ]
  ^CTraceback (most recent call last):
    File "/opt/monitiq-universal/install/modules/create_db_user-v0_0_3.py", line 132, in &lt;module&gt;
      inputVal = raw_input("");
  Traceback (most recent call last):
    File "monitiq_install.py", line 40, in &lt;module&gt;
  KeyboardInterrupt
      module_install.runModules();
    File "/opt/monitiq-universal/install/module_install.py", line 86, in runModules
      exit_code = runModule(module);
    File "/opt/monitiq-universal/install/module_install.py", line 19, in runModule
      for line in iter(process.stdout.readline, b''):
  KeyboardInterrupt
  </code></pre>
  
  <p>A solution or some pointers would be helpful :)</p>
  
  <p><strong><em>--EDIT</em></strong>
  With try catch</p>
  
  <pre><code>Running Module: 'create_db_user' Version: '0.0.0'
  Choose username for Monitiq DB User [MONITIQ]
  ^CGot keyboard interupt!
  Traceback (most recent call last):
    File "monitiq_install.py", line 36, in &lt;module&gt;
      module_install.runModules();
    File "/opt/monitiq-universal/install/module_install.py", line 90, in runModules
      exit_code = runModule(module);
    File "/opt/monitiq-universal/install/module_install.py", line 29, in runModule
      sys.exit(0);
  NameError: global name 'sys' is not defined
  Traceback (most recent call last):
    File "/opt/monitiq-universal/install/modules/create_db_user-v0_0_0.py", line 132, in &lt;module&gt;
      inputVal = raw_input("");
  KeyboardInterrupt
  </code></pre>