Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 206
Vicky Avatar asked Jan 23 '23 18:01

Vicky


1 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")
like image 157
luc Avatar answered Jan 31 '23 21:01

luc