Windows process management using Python
I need a script that check if a particular process is running and return something if not found. I know that t开发者_JAVA百科his can be done using subprocess, but is there a simpler way to do it?
On Windows, you can use WMI:
import win32com.client
def find_process(name):
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(".", "root\cimv2")
colItems = objSWbemServices.ExecQuery(
"Select * from Win32_Process where Caption = '{0}'".format(name))
return len(colItems)
print find_process("SciTE.exe")
Take a look at: getting process information on windows
For similar purposes I have used psutil library. Some hints:
- list processes with
psutil.pids()
(reference) - inspect process information with
process = psutil.Process(pid)
(reference) - do
process.kill
orprocess.terminate()
Installation on windows - pip
will do installation from the source (which means compiling), so you probably want to download binary installation from https://pypi.python.org/pypi/psutil/#downloads.
精彩评论