This page looks best with JavaScript enabled

Checking processes with Python

 ·  🎃 kr0m

In this occasion, we will learn how to search for certain processes on a computer using the psutil library in Python. It is as simple as importing the psutil library, going through all the processes, and comparing proc.name with the desired value. But first, we install the python library:

emerge -av dev-python/psutil

Now, we generate the following script:

#! /usr/bin/python
import psutil

dbs = {  "mysql" : 0,
         "redis" : 0 }

for proc in psutil.process_iter():
    for db in dbs.keys():
        if db in proc.name:
            dbs[db] = 1

for db in dbs.keys():
    print "Service " + db + " --> " + str(dbs[db])

In this example, we search for the processes of MySQL and Redis, and to store the result, we use an associative array.

If you liked the article, you can treat me to a RedBull here