Do you want to determine whether a process is running or not by name and find its Process ID as well? Then, this is the right place as this article will shed light on you completely on how to check if a process is running or not using Name. We have provided enough examples for finding the Process ID PID by Name for better understanding. You can visit our site online to seek guidance or simply copy-paste the code from our portal and try to apply this knowledge and write a code on your own.
- How to Check if there exists a Process running by Name and find its Process ID (PID)?
- Determine if Process is Running or Not
- Finding the Process ID(PID) by Name?
How to Check if there exists a Process running by Name and find its Process ID (PID)?
In this article, we will discuss finding a running process PIDs by name using psutil
.
psutil
library can be installed by using
pip install psutil
Determine if Process is Running or Not
Follow the simple guidelines for checking if a process is running or not. They are as such
- def findProcessIdByName(processName):
- for proc in psutil. process_iter():
- pinfo = proc. as_dict(attrs=[‘pid’, ‘name’, ‘create_time’])
- if processName. lower() in pinfo[‘name’]. lower() :
- except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
To check if a process is running or not we will iterate over all the running processes using psutil.process_iter()
and during iteration match the process name.
So let’s see this example to check chrome process is running or not.
import psutil def checkProcessRunning(processName): # Checking if there is any running process that contains the given name processName. #Iterate over the all the running process for proc in psutil.process_iter(): try: # Check if process name contains the given name string. if processName.lower() in proc.name().lower(): return True except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass return False; # Checking if any chrome process is running or not. if checkProcessRunning('chrome'): print('Yes chrome process was running') else: print('No chrome process was running')
Output:
No chrome process was running
As in my system chrome instances are not running it will return False.
Finding the Process ID(PID) by Name?
In the above program, we checked any chrome instances were running or not. But in this example, we will see how to get the Process ID of a running ‘chrome.exe’ process. For that we will iterate over all the running processes and during iteration for each process whose name contains the given string, we will keep its information in a list. The following code helps you find the PID of a running process by Name.
import psutil def findProcessIdByName(processName): # Here is the list of all the PIDs of a all the running process # whose name contains the given string processName listOfProcessObjects = [] #Iterating over the all the running process for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time']) # Checking if process name contains the given name string. if processName.lower() in pinfo['name'].lower() : listOfProcessObjects.append(pinfo) except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) : pass return listOfProcessObjects; # Finding PIDs od all the running instances of process # which contains 'chrome' in it's name listOfProcessIds = findProcessIdByName('chrome') if len(listOfProcessIds) > 0: print('Process Exists | PID and other details are') for elem in listOfProcessIds: processID = elem['pid'] processName = elem['name'] processCreationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(elem['create_time'])) print((processID ,processName,processCreationTime )) else : print('No Running Process found with this text')
Now, the contents of the list
(2604, 'chrome.exe', '2018-11-10 19:12:13') (4276, 'chrome.exe', '2018-11-10 19:12:14') (9136, 'chrome.exe', '2018-11-10 19:12:14') (9616, 'chrome.exe', '2018-11-10 19:43:41') (12904, 'chrome.exe', '2018-11-10 19:12:13') (13476, 'chrome.exe', '2018-11-10 20:03:04') (15520, 'chrome.exe', '2018-11-10 20:02:22')
This can also be done using a single line using list comprehension i.e
# Finding the PIDs of all the running instances of process that contains 'chrome' in it's name procObjList = [procObj for procObj in psutil.process_iter() if 'chrome' in procObj.name().lower() ]
And the same output will come like
psutil.Process(pid=2604, name='chrome.exe', started='19:12:13') psutil.Process(pid=4276, name='chrome.exe', started='19:12:14') psutil.Process(pid=9136, name='chrome.exe', started='19:12:14') psutil.Process(pid=9616, name='chrome.exe', started='19:43:41') psutil.Process(pid=12904, name='chrome.exe', started='19:12:13') psutil.Process(pid=13476, name='chrome.exe', started='20:03:04') psutil.Process(pid=15520, name='chrome.exe', started='20:02:22')
Related Programs:
- Python Program to Map Two Lists into a Dictionary
- Python Program to Remove Adjacent Duplicate Characters from a String
- Python Check if Two Lists are Equal
- Python Program to Merge two Lists and Sort it
- Python Program for Multiplication of Two Matrices