Connecting Tech Pros Worldwide Forums | Help | Site Map

Using python to check the status of a program

Kamuela Franco
Guest
 
Posts: n/a
#1: Jul 18 '05
I would like to use a Python script to periodically check to see if a
program is still running and if it isn't I want to start it up. Could
someone point me on the right path? Thanks in advance.

Kamuela

John J. Lee
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Using python to check the status of a program


Kamuela Franco <kamuelaf@verizon.net> writes:
[color=blue]
> I would like to use a Python script to periodically check to see if a
> program is still running and if it isn't I want to start it up. Could
> someone point me on the right path? Thanks in advance.[/color]

Search the ASPN Python Cookbook for "watchdog" -- I remember a recipe
there (search c.l.py with Google Groups too, of course).


John
The Jetman
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Using python to check the status of a program


Kamuela Franco <kamuelaf@verizon.net> wrote in message news:<pan.2003.12.27.10.53.23.520358@verizon.net>. ..[color=blue]
> I would like to use a Python script to periodically check to see if a
> program is still running and if it isn't I want to start it up. Could
> someone point me on the right path? Thanks in advance.
>
> Kamuela[/color]

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....

If we're talking about Windows, then this would work using the
Cygwin utils for ps or one could try Pythonwin. Mark has ported
many Windows APIs, so it would be a little more involved, but
straight-fwd. HTH....Jet
Miki Tebeka
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Using python to check the status of a program


Hello Jetman,[color=blue][color=green]
> > I would like to use a Python script to periodically check to see if a
> > program is still running and if it isn't I want to start it up. Could
> > someone point me on the right path? Thanks in advance.[/color]
> If we're talking about Windows, then this would work using the
> Cygwin utils for ps or one could try Pythonwin. Mark has ported
> many Windows APIs, so it would be a little more involved, but
> straight-fwd. HTH....Jet[/color]
There are always the pstools from sysinternals
(http://www.sysinternals.com/ntw2k/fr.../pstools.shtml)

HTH.
Miki
David M. Wilson
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Using python to check the status of a program


jetman516@hotmail.com (The Jetman) wrote...
[color=blue]
> 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....[/color]

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.
J.R.
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Using python to check the status of a program



"The Jetman" <jetman516@hotmail.com> wrote in message
news:a38d43b0.0312271424.70a4138c@posting.google.c om...[color=blue]
>
> 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....[/color]

If the process id is known, use "kill -0 pid" to check if the process is
still running.
Additional, you may need to check if it's coring even the return value of
the kill command is not 0.


Closed Thread