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

2

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Why the semicolon and nonstandard names? Variable and function names should generally follow the lower_case_with_underscores style.
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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.