Processes in Linux from Python

Wojtek Walczak gminick at bzt.bzt
Mon Sep 1 12:39:14 CEST 2008

On Sun, 31 Aug 2008 23:25:56 -0700 (PDT), Johny wrote:
> To get a number of  the http processes running on my Linux( Debia box)
> I use
> ps -ef | grep "[h]ttpd" | wc -l
> So my question is:
> Is it possible to get a number of the http processes running on Linux
> directly from Python ?

Yes, it is. There is a number of third party packages that provide
such functionality, including PSI:
I never actually liked them, because they are parsing /proc directory
by themselves. Thus I wrote my own tool that is a wrapper around
procps library (libproc* in your /lib, probably). Your system tools
like ps, w or top are using this library. My wrapping library is
available at:
In your case you could use it like this:

>>> import procpy
>>> pp = procpy.Proc()
>>> for pid in pp.pids:
...    if pp.procs[pid]['cmd'] == 'apache2':
...       print pp.procs[pid]['tid']

it prints the PIDs of all the apache2 processes on my system.
Procpy is fine for my own needs, but if I were about to write
a code that is intended to be portable, I'd use PSI. It also is
more mature.

Wojtek Walczak,

More information about the Python-list mailing list