[Tutor] capturing process's status with python

Eric Brunson brunson at brunson.com
Fri Aug 24 00:05:41 CEST 2007

I'm a bit more of a Linux expert than a Python guru, so I don't know if 
there's a facility in python to do that.  However, if you look in /proc, 
every running process has a directory corresponding to its PID and in 
that directory is a pseudo-file called "status" that you can get state 
information from:

foxtrot(~)$ grep State: /proc/*/status | head -10
/proc/10065/status:State:       S (sleeping)
/proc/10125/status:State:       S (sleeping)
/proc/10793/status:State:       S (sleeping)
/proc/10801/status:State:       S (sleeping)
/proc/10/status:State:  S (sleeping)
/proc/1125/status:State:        S (sleeping)
/proc/1126/status:State:        S (sleeping)
/proc/1128/status:State:        S (sleeping)
/proc/1129/status:State:        S (sleeping)
/proc/11/status:State:  S (sleeping)

Convert that to python and you're in:

import glob

for file in glob.glob( '/proc/*/status' ):
    f = open( file )
    for line in f:
        if line.startswith( 'State:' ):
            junk, state, text = line.split( None, 2 )
            print state, text

I'm interested to know if there's a portable python interface to the 
kernel process table, though, because that is very Linux specific.

Hope that helps,

Flaper87 wrote:
> Hi everybody!
> i would like to know how can i capture the system process's status 
> with python, i need to know if they are sleeping, zombie or runing.
> I use Debian lenny, GNU/Linux
> thanks
> -- 
> Flavio Percoco Premoli, A.K.A. [Flaper87]
> http://www.flaper87.com
> Usuario Linux registrado #436538
> Geek by nature, Linux by choice, Debian of course.
> Key Fingerprint: CFC0 C67D FF73 463B 7E55  CF43 25D1 E75B E2DB 15C7
> ------------------------------------------------------------------------
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list