Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a particular Python script is already running

How can I check, from a Python script, if another specific Python script is running?

def running():
    for q in psutil.process_iter():
        print q
        if q.name() == 'server_class.py':
            return True
    return False

I have tried that but the program is called "python" and not the name of the script.

like image 325
saar13531 Avatar asked Mar 24 '26 09:03

saar13531


1 Answers

The python script file will be part of the command line. You can try something along these lines:

def running():

    for q in psutil.process_iter():
        if q.name() == 'python':
            print q.cmdline()
            if len(q.cmdline())>1 and 'server_class.py' in q.cmdline()[1]:
                return True

    return False

I am using in just for the example. You might want to match the full path.

like image 141
Juan Leni Avatar answered Mar 25 '26 23:03

Juan Leni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!