In this article we will discuss a cross platform way to find a running process PIDs by name using psutil.

To install python’s psutil library use,

Check if a process is running

To check if process is running or not, let’s iterate over all the running process using psutil.process_iter() and match the process name i.e.

Let’s use this function to check if any process with ‘chrome’ substring in name is running or not i.e.

Output:

As in my system many chrome instances are running. So it will return True. But how to get the process ID of all the running ‘chrome.exe’ process ?

Find PID (Process ID) of a running process by Name

As there may be many running instances of a given process. So, we will iterate over all the running process and for each process whose name contains the given string, we will keep it’s info in a list i.e.

Let’s call this function to get the PIDs of all the running chrome.exe process

Contents of the list will be,

We can do the same in a single line using list comprehension i.e.

procObjList is a list of Process class objects. Let’s iterate over it and print them i.e.

Output will be,

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.