qed
qed

Reputation: 23104

In linux, how to use python to check whether an external program is running?

In linux, how to use python to check whether an external non-system program is running? Is there a standard module for this task?

Upvotes: 1

Views: 102

Answers (2)

Zaur Nasibov
Zaur Nasibov

Reputation: 22659

Use psutil, e.g.

import psutil

def check_if_running(name):
    for ps in psutil.process_iter():
        if ps.name == name:
            return True
    return False

check_if_running('python')
>>> True

Upvotes: 2

ziu
ziu

Reputation: 2720

You could be interested in psutil. Otherwise you could invoke pgrep using the subprocess library.

Upvotes: 0

Related Questions