Re: Writing a well-behaved daemon

Discussion in 'Python' started by Sean DiZazzo, Sep 26, 2008.

  1. Sean DiZazzo

    Sean DiZazzo Guest

    On Sep 25, 10:08 pm, Ben Finney <> 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/bda.daemon/trunk/bda/daemon/daemon.py

    ~Sean
     
    Sean DiZazzo, Sep 26, 2008
    #1
    1. Advertising

  2. Sean DiZazzo

    Sean DiZazzo Guest

    On Sep 26, 12:13 am, Ben Finney <>
    wrote:
    > Sean DiZazzo <> writes:
    > > Looks like somebody did the same thing I did and posted it.

    >
    > >http://svn.plone.org/svn/collective/bda.daemon/trunk/bda/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.
     
    Sean DiZazzo, Sep 26, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Muddy Coder

    Why WTK behaved weirdly?

    Muddy Coder, Jul 29, 2008, in forum: Java
    Replies:
    0
    Views:
    294
    Muddy Coder
    Jul 29, 2008
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    465
    Jean-Paul Calderone
    Mar 20, 2009
  3. Floris Bruynooghe
    Replies:
    1
    Views:
    460
    Floris Bruynooghe
    Mar 24, 2009
  4. Replies:
    1
    Views:
    108
    Eric Hodel
    Nov 11, 2005
  5. Daemon Win32::Daemon;

    , Sep 7, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    261
Loading...

Share This Page