472,354 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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
5 6997
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The...
1
by: Johhny | last post by:
Hello, I am currently looking to write a utility in python that will monitor the statis of a RAID card within linux. The card in Question is the...
0
by: | last post by:
Greetings. In an effort to get python2.4 on my Centos 3.7, I installed the python bootstrap rpm. This installed 2.4 alongside 2.2 and updated...
12
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to...
7
by: wardm | last post by:
I have created a Dict object in a C++ App that calls (embedded) Python functions. The Dict is to be used to pass variable data between the C++ App...
6
by: yaru22 | last post by:
I'd like to create a program that validates bunch of urls against the w3c markup validator (http://validator.w3.org/) and store the result in a...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no...
3
by: RossGK | last post by:
I'm a newbie to python threads, and playing with some simple client server stuff and lots of print statements. My server thread launched with...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.