Killing subprocess created with shell=True passed to subprocess.Popen‘s constructor under Linux kills only shell and not the process created by shell (see How to terminate a python subprocess launched with shell=True). However python -c "import subprocess; subprocess.Popen(['ping', '-t', 'google.com'], shell=True).terminate()" run under Windows Vista x64 SP3 and Python 2.7.3 64bit kills the ping process. Under Windows subprocess.Popen.terminate() calls TerminateProcess. However documentation of this function states
Terminates the specified process and all of its threads.
There is no mention about terminating whole process tree. What am I missing?
I think this is only with the one-liner you give, and my observations suggest that the
pingdoes not even get started. If you run as a script (Windows 7):Then the
proc.terminate()only terminates the shell, it does not terminate theping!However, if you set
shell=Falsethen it behaves as expected – it terminates theping. Same behaviour on Python 2.7 and 3.2.Edit: I tried this code as a one-liner as well, and got the same results as the questioner. I hate
sleephacks, but this works: