Will killing Python script that called shell processes also kill the shell processes?
Question: If I have some code like this in the file this_script.py:

    
    
    import subprocess
    subprocess.Popen(["python", "another_script.py"])
    

and I call

    
    
    python this_script.py
    

and kill the process while it is running, will it kill the subprocess?

Edit: I tested this, and if this_script is killed, the subprocess continues
running. Is there a way to make sure that the background process dies when the
main Python process does?


Answer: Yes, you can catch `KeyboardInterrupt`, and `SystemExit` and make sure to
`kill` the subprocess.

    
    
    from subprocess import Popen
    
    try:
        p = Popen(args)
        p.wait()  # wait for the process to finish
    except KeyboardInterrupt,  SystemExit:
        p.kill()
        raise
    

