From: Thomas Allen on
I have a script that runs an instance of SimpleXMLRPCServer and in
general it works as expected. In its __del__, it is supposed to clean
up its PID file (written on boot). I have two problems with this
server instance: The first is that tt doesn't always clean up its PID
file; is there a more reliable way to do this than how I am currently?
The second is that when it does crash, I don't know about it...what
would be sufficient as a "keep-alive" script to restart it? I suppose
I could use something like EventMachine (already installed on my
server) to watch the PID file if it were deleted reliably.

Thomas Allen
From: Adam Tauno Williams on
On Fri, 2010-01-29 at 07:54 -0800, Thomas Allen wrote:
> I have a script that runs an instance of SimpleXMLRPCServer and in
> general it works as expected. In its __del__, it is supposed to clean
> up its PID file (written on boot). I have two problems with this
> server instance: The first is that tt doesn't always clean up its PID
> file; is there a more reliable way to do this than how I am currently?
> The second is that when it does crash, I don't know about it...what
> would be sufficient as a "keep-alive" script to restart it? I suppose
> I could use something like EventMachine (already installed on my
> server) to watch the PID file if it were deleted reliably.

Why don't you wrap the server in a try/except block? Then if an
exception occurs killing the server you can automatically restart and/or
send an e-mail to an admin address.

From: Tim Wintle on
On Fri, 2010-01-29 at 07:54 -0800, Thomas Allen wrote:
> The second is that when it does crash, I don't know about it...what
> would be sufficient as a "keep-alive" script to restart it? I suppose
> I could use something like EventMachine (already installed on my
> server) to watch the PID file if it were deleted reliably.

If the server crashes then it clearly won't get around to deleting it's
pid file.

The way I do it is to use os.kill (with signal 0 IIRC) to check if the
process is still alive when the script starts. If it's not then I delete
the pid file and carry on starting up.

Tim

From: Sean DiZazzo on
On Jan 29, 7:54 am, Thomas Allen <thomasmal...(a)gmail.com> wrote:
> I have a script that runs an instance of SimpleXMLRPCServer and in
> general it works as expected. In its __del__, it is supposed to clean
> up its PID file (written on boot). I have two problems with this
> server instance: The first is that tt doesn't always clean up its PID
> file; is there a more reliable way to do this than how I am currently?
> The second is that when it does crash, I don't know about it...what
> would be sufficient as a "keep-alive" script to restart it? I suppose
> I could use something like EventMachine (already installed on my
> server) to watch the PID file if it were deleted reliably.
>
> Thomas Allen

You should check out python-daemon. I use home grown daemons all the
time, and have only played a little with the python-daemon library,
but it is surely written well, and handles lockfiles and pidfiles.

I believe you can map a function to any signal that the daemon
receives to do any kind of cleanup etc. Ssometimes those PID files
might be left around, but the runner included with the module (i
think) checks for stale pid files.

There's all kinds of useful stuff.

In my home grown daemons I use Adam's technique, and wrap the "while
1:" block in a try except clause.

~Sean
From: Gabriel Genellina on
En Fri, 29 Jan 2010 12:54:23 -0300, Thomas Allen <thomasmallen(a)gmail.com>
escribi�:

> I have a script that runs an instance of SimpleXMLRPCServer and in
> general it works as expected. In its __del__, it is supposed to clean
> up its PID file (written on boot). I have two problems with this
> server instance: The first is that tt doesn't always clean up its PID
> file; is there a more reliable way to do this than how I am currently?
> The second is that when it does crash, I don't know about it...what
> would be sufficient as a "keep-alive" script to restart it? I suppose
> I could use something like EventMachine (already installed on my
> server) to watch the PID file if it were deleted reliably.

I agree with the recommendation of using some daemon library; doing it
right is better left to the experts :)
But if you can't or don't want to alter your code so much, I suggest:

- use atexit.register, instead of __del__, to delete the PID file
- keep a lock on the pid file; if a second instance is able to write to
it, it means it's an orphan from a previous, aborted run. (Checking
whether a process with such pid exists is not enough - pids are recycled
rather fast)

--
Gabriel Genellina