title: Killing a subprocess including its children from python
body: <p>I'm using the subprocess module on python 2.5 to spawn a java program (the selenium server, to be precise) as follows:</p>
  
  <pre><code>import os
  import subprocess
  
  display = 0
  log_file_path = "/tmp/selenium_log.txt"
  selenium_port = 4455
  selenium_folder_path = "/wherever/selenium/lies"
  
  env = os.environ
  env["DISPLAY"] = ":%d.0" % display
  command = ["java", 
             "-server",
             "-jar", 
             'selenium-server.jar',
             "-port %d" % selenium_port]
  log = open(log_file_path, 'a')
  comm = ' '.join(command)
  selenium_server_process = subprocess.Popen(comm,
                                             cwd=selenium_folder_path,
                                             stdout=log,
                                             stderr=log,
                                             env=env,
                                             shell=True)
  </code></pre>
  
  <p>This process is supposed to get killed once the automated tests are finished. I'm using <code>os.kill</code> to do this:</p>
  
  <pre><code>os.killpg(selenium_server_process.pid, signal.SIGTERM)
  selenium_server_process.wait()
  </code></pre>
  
  <p>This does not work. The reason is that the shell subprocess spawns another process for java, and the pid of that process is unknown to my python code. I've tried killing the process group with <code>os.killpg</code>, but that kills also the python process which runs this code in the first place. Setting shell to false, thus avoiding java to run inside a shell environment, is also out of the question, due to other reasons. </p>
  
  <p>How can I kill the shell and any other processes generated by it?</p>