How to check if a program or process is running in python

When automating tasks in python or other programming languages, it is frequently handy to be able to determine if a given program or process is already running. You may want to check that you only have one version of your script running at a time, or for selenium webdriver you don’t want multiple browsers trying to open the same profile and cause conflicts.

I have several automated scripts that I use to collect data from websites that I don’t want running at the same time, but are scheduled to run at random intervals throughout the day. In these cases if two of them run at the same time, issues with webdriver occur and they will both crap out.

To avoid this problem, I found a simple python function to check if an application, program, or process is currently running. I found this buried on some other programming site a few months ago and was not able to find it again. I can’t take credit for this code, if you can find the original, send me a message so I can give credit.


Here is the function. It requires the psutil library. It works with Windows, for sure, I think it would work on Linux. Not sure about Mac OS.

import psutil
def checkIfProcessRunning(processName):
        '''Check if there are any running process that contains the given name processName.
        Iterate over the all the running process'''
        print('Checking if application is running...')
        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;

We pass the function a process name (e.g. Chrome). It then iterates through the list of active processes running on the system. If it finds a match, it will return True. If no match, it returns False.

I set this function call in a while loop to determine if Chrome is running. See the example code below:

import time
chromeRunning = True
while chromeRunning:
    if checkIfProcessRunning('chrome'):
        print('Instance of Chrome is Running...waiting.')
        time.sleep(30) # Wait 30 seconds and try again
        print('Checking again...')
    else:
        print('Chrome is not running, good to go.')
        chromeRunning = False # Sets chromeRunning False to exit loop

In the code above, we set a chromeRunning = True to setup the while loop. The while loop runs, calls the checkIfProcessRunning for ‘chrome’. If the function returns true, it waits 30 seconds and tries again.

Once checkIfProcessRunning returns False, it sets chromeRunning = False to exit the while loop and continue with the rest of the program.

You need to determine the process name you’re checking. You can do this on Windows through the Task Manger or on Linux/Mac OS using “top” in the terminal.


Complete program below:

import time, psutil
def checkIfProcessRunning(processName):
        '''Check if there are any running process that contains the given name processName.
        Iterate over the all the running process'''
        print('Checking if application is running...')
        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;

chromeRunning = True
while chromeRunning:
    if checkIfProcessRunning('chrome'):
        print('Instance of Chrome is Running...waiting.')
        time.sleep(30) # Wait 30 seconds and try again
        print('Checking again...')
    else:
        print('Chrome is not running, good to go.')
        chromeRunning = False # Sets chromeRunning False to exit loop

I hope you find this python function to check if a process or program is running useful! Let me know what you think or if you have a more efficient method for performing this check. Again, if you know the original author of the function, let me know so I can give credit.

John

Leave a Reply

Your email address will not be published. Required fields are marked *