Using python to check the status of a program

David M. Wilson dw-google.com at botanicus.net
Sun Dec 28 09:59:54 EST 2003


jetman516 at hotmail.com (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
http://cr.yp.to/, and many more at http://freshmeat.net/.

David.




More information about the Python-list mailing list