Python daemon

Dave Swegen dswegen at software.plasmon.com
Tue Jun 25 05:52:49 EDT 2002


On Tue, Jun 25, 2002 at 10:46:24AM +0200, ?????? ?. wrote:
> Hello, 
> 
> I'm writing a Python daemon on Unix and I need the folowing features:
>  1. going to background, diassociate from controling tty (done)

At this point I'd save the PID of the daemon into a file in
/var/run/<daemonname>.pid, say.

>  2. watchdog, the program should never be stuck

Have a shell script called by cron that checks that the PID still exists
in the output of /bin/ps, and have it called at appropriate intervals.
Or instead of using cron call both the watcher and the daemon from a
starter script. Personally I'd go with the cron option.

The above solution would work if the daemon went away, but not if the
it hung. If that might happen have the daemon update the timestamp on
the pid file at a regular interval (say each iteration of a main loop).
Then use the timestamp to determine if the daemon seems to have hung or
not.

This is a simple but quite robust way, that I would think should be
robust enough for most apps. If that isn't finegrained enough you could
look at using named pipes, signals or a gazillion other methods of
communicating with the daemon.

Perhaps there is a more pythonic way of doing it, but this is how I'd do
it.

>  3. restarting of the program if it fails, or the watchdog doesn't trigger

Use a wrapper script that starts, stops (using the pidfile) and restarts the
daemon. The wrapper can then be called at bootime or by the watcher
script.

>  4. Logging

Don't know what the best python way of doing this is.

> 
> 
> The issue 1. I solved, it's on ActiveState's site. Issue 4. is not that
> hard to do I guess. Any suggestions about 2 and 3 ?
> 
> 
> 
> -- 
> ??????
> 
>    "One world, one web, one program"  -- Microsoft promotional ad
>          "Ein Volk, ein Reich, ein Fuehrer"  -- Adolf Hitler
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list