user4719989
user4719989

Reputation: 127

Is there a way to check if a specific program is running (using Python)?

How can i check if a specific python file is running through another python file? for example in python file1 there will be a condition:

if file.IsRunning:

i have tried to do:

__author__ = 'Cyber-01'
import psutil

def IsRunning(name):
    for pid in psutil.pids():
        try:
            p = psutil.Process(pid)
            if len(p.cmdline())>1:
                if name in p.cmdline()[1]:
                    return True
        except:
            None
    return False

if running("server.py"):
    print "yes"

but it always returns yes no matter

Upvotes: 0

Views: 5769

Answers (2)

Riley Martin
Riley Martin

Reputation: 21

A simple way to do this is:

import psutil
print("Roblox" in (i.name() for i in psutil.process_iter()))

Upvotes: 1

Montmons
Montmons

Reputation: 1426

First-attempt:

import psutil

def running(name):
    name_list = []
    for proc in psutil.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['pid', 'name'])
            if name in pinfo['name']:
                return True
            else:
                pass
        except:
            return False

EDIT

A solution more in line with the OP's original post:

def running3(program, scriptname):
    for pid in psutil.pids(): # Iterates over all process-ID's found by psutil,  
        try:
            p = psutil.Process(pid) # Requests the process information corresponding to each process-ID, the output wil look (for example) like this: <psutil.Process(pid=5269, name='Python') at 4320652312>
            if program in p.name(): # checks if the value of the program-variable that was used to call the function matches the name field of the plutil.Process(pid) output (see one line above). So it basically calls <psutil.Process(pid=5269, name='Python') at 4320652312>.name() which is --> 'Python'
                """Check the output of p.name() on your system. For some systems it might print just the program (e.g. Safari or Python) on others it might also print the extensions (e.g. Safari.app or Python.py)."""
                for arg in p.cmdline(): # p.cmdline() echo's the exact command line via which p was called. So it resembles <psutil.Process(pid=5269, name='Python') at 4320652312>.cmdline() which results in (for example): ['/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python', 'Start.py'], it then iterates over is splitting the arguments. So in the example you will get 2 results: 1 = '/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python' and 2 = 'Start.py'. It will check if the given script name is in any of these.. if so, the function will return True.
                    if scriptname in str(arg):  
                        return True
                    else:
                        pass
            else:
                pass
        except:
            continue

if running3('Python', 'server.py'):
    print("Yes, the specified script is running!")
else:
    print("Nope, can't find the damn process..")

As to why the OP's code wasn't working:

I can see a couple of wrongs in the code. First of all, the indentation of return False places it outside the for-loop, thereby creating a situation in which the function will always return False.

Furthermore the psutil.Process.cmdline() returns the command that was used to execute the current process. When psutil.Process.cmdline(1) is called it is supposed to return the command line via which the process of PID 1 was called. However, this seems to throw up a lot of permission errors. This, in turn, causes if len(p.cmdline())>1 to fail almost all of the time, raising the exception and thus returning None in the OP's code.

Second EDIT (tested the two solutions for optimal performance)

  • Code 1 (using psutil.process_iter()): 100 loops, best of 3: 4.37 msec per loop
  • Code 2 (using psutil.Process(pid).name()): 1000 loops, best of 3: 945 usec per loop
  • Conclusion: the 2nd code seems to be your best bet!

Upvotes: 4

Related Questions