467,074 Members | 1,089 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

Using python to check the status of a program

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
Jul 18 '05 #1
  • viewed: 6620
Share:
5 Replies
Kamuela Franco <ka******@verizon.net> writes:
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.


Search the ASPN Python Cookbook for "watchdog" -- I remember a recipe
there (search c.l.py with Google Groups too, of course).
John
Jul 18 '05 #2
Kamuela Franco <ka******@verizon.net> wrote in message news:<pa****************************@verizon.net>. ..
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


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
Jul 18 '05 #3
Hello Jetman,
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.

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

There are always the pstools from sysinternals
(http://www.sysinternals.com/ntw2k/fr.../pstools.shtml)

HTH.
Miki
Jul 18 '05 #4
je*******@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.
Jul 18 '05 #5

"The Jetman" <je*******@hotmail.com> wrote in message
news:a3**************************@posting.google.c om...

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 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.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Johhny | last post: by
reply views Thread by | last post: by
12 posts views Thread by tshad | last post: by
852 posts views Thread by Mark Tarver | last post: by
3 posts views Thread by RossGK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.