Want to monitor process..

Stephen shriek at gmx.co.uk
Thu Feb 28 12:01:45 EST 2002


> > At present I use a naive way to write a pid file when initiate the
> > process, but in that case I can't restart it when the process has been
> > down. (i.e., if that when the process has gone, I should kill the
> > processes and remove pid file manually.)

I'm chuckling to myself. This is exactly what I'm doing at the moment
too and it sucks. I'm so glad you asked this question, Haeyoung, and
described it so well :)


> Check out the module "atexit". You can set it up so that when the process 
> is dying, it will call the functions. One of the functions should remove 
> the pid lock file.

Unfortunately, my own applications seem to crash as opposed to
shutdown normally, so it's probably not going to help me. From the
library reference ~
"Note: the functions registered via this module are not called when
the program is killed by a signal, when a Python fatal internal error
is detected, or when os._exit() is called. "


Thanks for the great examples and explanations, Jonathan. 


> > I want to run or kill a process(program?) using python.
> 
> from os import *
> # Start a process
> ...
> pid = fork()
> if not pid:
>         # I'm a child!
>         exec*(what you want to do)
> # I'm a parent
> ...


In this case, though, if the parent process were to die, then we lose
all control.  In my own scenario I have ~

1. A SocketServer application written in Python.  This server tends to
crash quite often for reasons I'm still trying to debug.

However, even once I have debugged most the problems, I'm quite
paranoid and would like to have a totally second program ~

2. A Python program that wakes up (eg. on cronjob) every minute and
checks if the SocketServer application is running.

My current method of doing this is to write the pid to a file in (1.)
and then the monitoring program (2.) does a "ps aux" and checks if the
process in the file is running. But I really don't like this approach.

Is there a lightweight IPC mechanism that my monitoring program could
talk to the the socketserver. Yes, I could connect to the socket like
a client, but that wouldn't actually tell me if the process is
running, but rather if the server is accepting connections (and my app
server is so flaky right now that it has been known to not accept
connections but not die yet).

Thank you, 

Stephen.



More information about the Python-list mailing list