473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7133
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 application(VB.NET) will receive the SMS automatically,...
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 LSI SAS1064 as the tools provided by the vendor...
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 yum to 2.4.0. Oddly, it didn't create a symlink...
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 see if this service is running. I know how 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 and the python functions. However I cannot get...
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 file. Since I don't know network programming, I...
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 axes to grind here. This is just a question for...
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 self.worker = WorkerThread(self) completes an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.