Check if process is running – Python: Check if a Process is Running by Name and find its Process ID (PID) | How to find the PID of a Running Process?

Check if process is running: 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)?

Python : get running processes: 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')

How to Check if a Process is Running or Not

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')

Finding Process ID by Name

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')

Answer these:

  1. How To Check If A Process Is Running In Python?
  2. Check If Process Is Running Python?
  3. Check If A Process Is Running Python?
  4. Check If Process Is Alive Python?
  5. How To Check If Python Process Is Running?

Related Programs:

Conclusion

I hope you may like the content or information in this article, by following this flow of content, more applications will come. And the flow like this, Psutil check if process is running by following this you have to check the python psutil check if process is running, python check if process is running, python script to check if process is running linux, Python check if process is running by pid, then python check if process is running by name, python check running processes, python get pid, python check if pid is running, Python check if Program is running, Python process check if running, Python find process by name, Python get process name, Python get process id.