import os
import signal
import subprocess
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, preexec_fn=
    os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
How to terminate a python subprocess launched with shell=True