0

I am trying to make a straightforward code where a variable = 0 if Instagram isn't running and = 1 if it is but I can't find a tool that can do this for me. I am relatively new to Python programming so most answers to similar questions that I've seen on this were confusing.

In short, is there a way to detect if an app/process such as Instagram is open on my computer?

How can I detect this using Python

  • Do you have anything? Have you done any research? – AMC Jan 19 '20 at 21:05
  • This looks like a duplicate of the following: https://stackoverflow.com/q/7787120/11301900, https://stackoverflow.com/q/1632234/11301900. – AMC Jan 19 '20 at 21:08

2 Answers2

1

check by process name, iterating on all processes

import psutil

def checkIfProcessRunning(processName):
    '''
    Check 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;

More intersting examples can be found here

Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
  • 1
    Why the semicolon and nonstandard names? Variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Jan 19 '20 at 21:09
0

Processes on UNIX based systems create a lock file to notify that a program is running, so os.stat(location_of_file) can be checked. If the file exists, the program is running, otherwise not.

Other than this, psutil works really fine in these kind of problems.

import psutil
b = psutils.pid_exists(pid)
if b:
   print ('Running')

This can be further checked at http://psutil.readthedocs.io/en/latest/#psutil.pid_exists

for p in psutil.process_iter(attrs=['pid', 'name']):
    if "instagram.exe" in (p.info['name']).lower():
        print ('Running')

On Windows, FindWindow can also be used.

import win32ui
if FindWindow ("Instagram", "Instagram"):
   print ('Running')
Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18