Entry
How can I kill a sub-process in Windows, from a python script ?
Aug 12th, 2006 23:32
Reed O'Brien, Probal Ghosh, http://www.python.org/doc/current/lib/module-subprocess.html
The subprocess Popen class has a number of useful objects
pid gives you the child's process id
poll() tells you if/how the child exited, None means it is still running.
To kill tasks in windows taskkill is the command line to kill tasks. PID
is the flag to do it by pid. /IM is the flag to kill gracefully by name.
And add the /F flag to force it.
w = subprocess.Popen("C:\\Python24\\python.exe", shell=False)
w.pid
4192
w.poll() == None
True
kill = subprocess.Popen("taskkill /PID %i" % w.pid, shell=True)
w = subprocess.Popen("C:\\Python24\\python.exe", shell=False)
kill = subprocess.Popen("taskkill /IM python.exe", shell=True)
or
kill = subprocess.Popen("taskkill /F /IM python.exe", shell=True)
kill.poll()
0
w.poll()
1
There is further discussion about process killing using win32 and ctypes
modules:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462