Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Writing a well-behaved daemon

Reply
Thread Tools

Re: Writing a well-behaved daemon

 
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-26-2008
On Sep 25, 10:08*pm, Ben Finney <(E-Mail Removed)> wrote:
> Howdy all,
>
> Writing a Python program to become a Unix daemon is relatively
> well-documented: there's a recipe for detaching the process and
> running in its own process group. However, there's much more to a Unix
> daemon than simply detaching. At a minimum, a well-behaved Unix daemon
> must at least:
>
> * Detach the process into its own process group
>
> * Close stdin, redirect stdout and stderr to a null device
>
> * Handle its own PID file: refuse to start if the PID file already
> * exists, write the PID file on startup otherwise, and remove the PID
> * file on termination
>
> * Revoke setuid and setgid privileges, which are strongly deprecated
> * these days
>
> * Handle interrupts, cleaning up the process and PID file as necessary
>
> * (possible others)
>
> There are also many other commonly-expected tasks that well-behaved
> Unix daemons perform (drop privileges to a nominated non-root user and
> group after daemonising, redirect output to syslog instead, operate in
> a chroot jail, respawn when terminated, etc.).
>
> All of this stuff has been done numerous times before, and the basics
> are mostly agreed upon. Yet all of these are tricky to implement
> correctly, and tedious to re-implement in every program intended to
> run as a daemon.
>
> The 'daemon' program <URL:http://www.libslack.org/daemon/> covers all
> these, and allows an arbitrary process to be started as a well-behaved
> Unix daemon process. It's not a good assumption that this program is
> installed on an arbitrary system though, and it seems excessive to ask
> the recipient of one's program to get a third-party program just in
> order to make a daemon process.
>
> I'd love to be able to have something similar for Python programs,
> e.g. a 'become_well_behaved_daemon()' function that implements all the
> above and perhaps takes optional arguments for the optional features.
>
> My searches for such functionality haven't borne much fruit though.
> Apart from scattered recipes, none of which cover all the essentials
> (let alone the optional features) of 'daemon', I can't find anything
> that could be relied upon. This is surprising, since I'd expect this
> in Python's standard library.
>
> Can anyone point me to the equivalent of the 'daemon' program in the
> form of a well-tested Python library?
>
> --
> *\ * * “Tis more blessed to give than to receive; for example, wedding |
> * `\ * * * * * * * * * * * * * * * * * * *presents.” —Henry L. Mencken |
> _o__) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *|
> Ben Finney


http://code.activestate.com/recipes/66012/

I actually use a brew of the original recipe combined with several of
the great comments. The commenters add alot of the functionality that
you are looking for. It works very well on most counts, but doesn't
handle being killed well. The daemons often last for a few weeks to a
month before something unexpected kills them off. If I was more
careful, I think they could live much longer.

Looks like somebody did the same thing I did and posted it.

http://svn.plone.org/svn/collective/...emon/daemon.py

~Sean
 
Reply With Quote
 
 
 
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-26-2008
On Sep 26, 12:13*am, Ben Finney <(E-Mail Removed)>
wrote:
> Sean DiZazzo <(E-Mail Removed)> writes:
> > Looks like somebody did the same thing I did and posted it.

>
> >http://svn.plone.org/svn/collective/...a/daemon/daemo...

>
> Thanks, I've not seen that before.
>
> It still seems loony to me that something this difficult to do right,
> yet so similar across different use cases, isn't in the standard
> library, where bugs need only be fixed in one place.
>
> --
> *\ * * * *“Pinky, are you pondering what I'm pondering?” “Wuh, I think |
> * `\ * * *so, Brain, but how will we get three pink flamingos into one |
> _o__) * * * * * * * * * * pair of Capri pants?” —_Pinky and The Brain_ |
> Ben Finney


+1 I think it would be a great addition.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing java daemon server: start and stop calvin01 Java 5 05-30-2009 05:39 PM
Re: PEP 3143: Standard daemon process library (was: Writing awell-behaved daemon) Floris Bruynooghe Python 1 03-24-2009 02:58 PM
Re: PEP 3143: Standard daemon process library (was: Writing awell-behaved daemon) Jean-Paul Calderone Python 0 03-20-2009 01:02 PM
Daemon Win32::Daemon; ph1975@gmail.com Perl Misc 0 09-07-2006 10:58 AM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM



Advertisments