开发者

Windows process management using Python

开发者 https://www.devze.com 2022-12-17 10:52 出处:网络
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 Wi

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 or 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号