How to get the active process list on a Linux machine using python
To retrieve the active process list on a Linux machine we will use the subprocess module . This module will allow us to create new subprocesses, connect to their input / output / error pipes, and eventually retrieve their exit codes .
It offers us a higher level and cleaner approach by defining only one class: subprocess.Popen(), intended to replace the functionality offered by methods such as: os.system(), os.popen() or os.popen2() .
To obtain the active processes and their associated information (USER, PID, CPU, MEM, TTY, STAT, TIME, COMMAND, etc.) we will spawn a ps aux subprocess, connect to its output pipe and parse the results .
But first let’s analyze a little the output of ps aux :
andrei@andrei:~$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 2872 1692 ? Ss 14:15 0:00 /sbin/init root 2 0.0 0.0 0 0 ? S 14:15 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 14:15 0:03 [ksoftirqd/0] root 4 0.0 0.0 0 0 ? S 14:15 0:00 [migration/0] root 5 0.0 0.0 0 0 ? S 14:15 0:00 [watchdog/0] root 6 0.0 0.0 0 0 ? S 14:15 0:00 [migration/1] root 7 0.0 0.0 0 0 ? S 14:15 0:00 [ksoftirqd/1] root 8 0.0 0.0 0 0 ? S 14:15 0:00 [watchdog/1] root 9 0.0 0.0 0 0 ? S 14:15 0:00 [events/0] root 10 0.0 0.0 0 0 ? S 14:15 0:00 [events/1] root 11 0.0 0.0 0 0 ? S 14:15 0:00 [cpuset] .....................................................................................................
Our first step will be to create a data structure capable of encapsulating the above information . We will write the Proc class that will wrap all the process meta-information present in the ‘ps aux‘ output :
#!/usr/bin/env python
from subprocess import Popen, PIPE
from re import split
from sys import stdout
class Proc(object):
''' Data structure for a processes . The class properties are
process attributes '''
def __init__(self, proc_info):
self.user = proc_info[0]
self.pid = proc_info[1]
self.cpu = proc_info[2]
self.mem = proc_info[3]
self.vsz = proc_info[4]
self.rss = proc_info[5]
self.tty = proc_info[6]
self.stat = proc_info[7]
self.start = proc_info[8]
self.time = proc_info[9]
self.cmd = proc_info[10]
def to_str(self):
''' Returns a string containing minimalistic info
about the process : user, pid, and command '''
return '%s %s %s' % (self.user, self.pid, self.cmd)
A list of Proc objects will probably do the job .