Listing processes with Python

Donn Cave donn at u.washington.edu
Thu May 22 11:49:59 EDT 2003


Quoth Matthew Knepley <knepley at mcs.anl.gov>:
|        Is there a portable way to obtain a process list in Python?

No.

| I looked in the OS module first but saw nothing. I tried the PythonCookbook,
| but the only relevant recipe (pykill) using os.system() to call 'ps' which
| is totally non-portable even among unices. I ma not sure how portable the
| /proc filesystem is.The purpose here is to implement something like killall.
| I want to get rid of runaway server daemons.

There are plenty of UNIX platforms with no /proc implementation, and
then where it's implemented it may be optional - for example, NetBSD
has had /proc for about a decade, and it's configured in the standard
issue kernel, but it is not mounted in the default configuration.  Or
it may be implemented in a totally different way, e.g., Digital UNIX
provides only a data file, presumably the process space.

So you just have to use whatever you get and deal with platform
novelties as they come along.  Of course the ideal thing is to
fix server daemons so they behave better, and the next best thing
is to make them store their process IDs in files in /var/run or
wherever is standard for the platform.  Then you still have the
problem of verifying that the actual process is the same one -
not another process created coincidentally with the same PID some
time after the original one exited.  That can be done with a
filesystem lock, or a process specific "ps" (still not very
portable of course, but the requirements are less rigorous here.)

	Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list