You'll have to move away from subprocess.call
to do it but it will still achieve the same results. Subprocess call runs the command passed, waits until completion then returns the returncode attribute. In my example I show how to get the returncode upon completion if needed.
Here's an example of how to kill a subprocess, you'll have to intercept the SIGINT (Ctrl+c) signal in order to terminate the subprocess before exiting the main process. It's also possible to get the standard output, standard error, and returncode attribute from the subprocess if needed.
#!/usr/bin/env python
import signal
import sys
import subprocess
def signal_handler(sig, frame):
p.terminate()
p.wait()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
p = subprocess.Popen('./stdout_stderr', shell=True,
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
# capture stdout and stderr
out, err = p.communicate()
# print the stdout, stderr, and subprocess return code
print(out)
print(err)
print(p.returncode)