Managing Windows Processes Using Python

I need a script that checks if a particular process is running and returns something if it is not found. I know this can be done using a subprocess, but is there an easier way to do this?

+3
source share
3 answers

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")
+6
source

psutil . :

  • psutil.pids() ()
  • process = psutil.Process(pid) ()
  • do process.kill process.terminate()

Installation on windows - pipwill perform the installation from the source code (which means compilation), so you probably want to download the binary installation from https://pypi.python.org/pypi/psutil/#downloads .

+1
source

Source: https://habr.com/ru/post/1728877/


All Articles