Using python to check the status of a program

David M. Wilson at
Sun Dec 28 15:59:54 CET 2003

jetman516 at (The Jetman) wrote...

> 1st, which OS are we talking about ?  *IF* we're talking about 
> Linux/Unix/FreeBSD, then it only takes a few lines:
>   import os
>   import string
>   f = os.popen( 'ps -ax' )
>   s = f.readlines()
>   if string.find( s, 'target_pgm' ) > -1:
>       ####  we found it, so do something....

I don't understand why so many people do this, grepping the process
list for strings is a terribly unreliable way of testing whether a
program is alive, especially when you don't have control of the
process list (eg. customer computers).

Might I suggest a modification to the procedure in my last post? Give
it an argument and pass that argument to ps' "-C" switch. Then test
for process existance with something like:

    if len(get_process_ids('yourbin')) == 0:
        # check for repeated crashes.
        # restart the server.

There are a multitude of programs for every imaginable OS to do this
for you already. Have you looked at them? There's one at, and many more at


More information about the Python-list mailing list