[英]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 this can be done using subprocess, but is there a simpler way to do it? 我知道这可以使用子进程完成,但是有更简单的方法吗?
On Windows, you can use WMI: 在Windows上,您可以使用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")
For similar purposes I have used psutil library. 出于类似的目的,我使用了psutil库。 Some hints: 一些提示:
psutil.pids()
( reference ) 使用psutil.pids()
列出进程( 参考 ) process = psutil.Process(pid)
( reference ) 使用process = psutil.Process(pid)
检查进程信息( 参考 ) process.kill
or process.terminate()
do process.kill
或process.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 . 在Windows上安装 - pip
将从源代码进行安装(这意味着编译),因此您可能希望从https://pypi.python.org/pypi/psutil/#downloads下载二进制安装。
看看: 在Windows上获取流程信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.