check for process name from python

Solutions on MaxInterview for check for process name from python by the best coders in the world

showing results for - "check for process name from python"
David
04 Jan 2020
1import psutil
2 
3def findProcessIdByName(processName):
4    '''
5    Get a list of all the PIDs of a all the running process whose name contains
6    the given string processName
7    '''
8 
9    listOfProcessObjects = []
10 
11    #Iterate over the all the running process
12    for proc in psutil.process_iter():
13       try:
14           pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
15           # Check if process name contains the given name string.
16           if processName.lower() in pinfo['name'].lower() :
17               listOfProcessObjects.append(pinfo)
18       except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
19           pass
20 
21    return listOfProcessObjects;
22