python-daemonize and upstart

Discussion in 'Python' started by Paul Rudin, Nov 14, 2009.

  1. Paul Rudin

    Paul Rudin Guest

    I'm experimenting with the daemon module
    <http://pypi.python.org/pypi/python-daemon/> and upstart
    <http://upstart.ubuntu.com/>. There's something I don't understand,
    which may be more of an upstart issue than a python issue, but I thought
    I'd start by posting here.

    Here's a test script:
    #!/usr/bin/python2.6
    import daemon
    import time

    def work():
    count = 0
    while True:
    with open('/tmp/test.txt', 'a') as f:
    f.write(str(count))
    f.write('\n')
    count += 1
    time.sleep(5)

    def main():
    with daemon.DaemonContext(working_directory='/tmp'):
    work()

    if __name__ == "__main__":
    main()

    and here's a testdaemon.conf upstart configuration:

    description "test daemon"
    expect daemon
    chdir /tmp
    exec /tmp/testdaemon.py

    If I do "sudo start testdaemon" I see the "testdaemon.py" process
    starting, and the file '/tmp/test.txt' is being written to every 5
    seconds, so everything has kicked off.

    The thing I don't understand is why start does not return. I guess it
    doesn't think that the process and properly started and daemonized
    itself? Quite possibly it's just that I don't understand this stuff
    well...
     
    Paul Rudin, Nov 14, 2009
    #1
    1. Advertising

  2. Paul Rudin

    Paul Rudin Guest

    Re: python-daemon and upstart

    Ben Finney <> writes:

    > Paul Rudin <> writes:
    >
    >> I'm experimenting with the daemon module
    >> <http://pypi.python.org/pypi/python-daemon/> and upstart
    >> <http://upstart.ubuntu.com/>.

    >
    > First: Thank you for using ‘python-daemon’; it's getting more widespread
    > use all the time, which is really helping to find all the quirks of API
    > and implementation. (And good for my ego at the same time.)
    >
    >

    Thanks for writing it.

    > There's something I don't understand, which may be more of an upstart
    >> issue than a python issue, but I thought I'd start by posting here.

    >
    > I'm unfamiliar with ‘upstart’, I hope others with more experience can
    > offer more insight.
    >


    On Karmic it seems to be the standard way for starting and stopping
    system processes.

    >> Here's a test script:

    > […]
    >
    > The program looks fine to me. What happens if you run the program,
    > without getting ‘upstart’ involved?
    >


    As for as I can tell everything is ok invoking the script from the
    command line.

    >> and here's a testdaemon.conf upstart configuration:
    >>
    >> description "test daemon"
    >> expect daemon
    >> chdir /tmp
    >> exec /tmp/testdaemon.py
    >>
    >> If I do "sudo start testdaemon" I see the "testdaemon.py" process
    >> starting, and the file '/tmp/test.txt' is being written to every 5
    >> seconds, so everything has kicked off.

    >
    > Good to know.
    >
    >> The thing I don't understand is why start does not return. I guess it
    >> doesn't think that the process and properly started and daemonized
    >> itself? Quite possibly it's just that I don't understand this stuff
    >> well...

    >
    > As I say, I'm completely unfamiliar with the details of ‘upstart’. Can
    > you point me to whatever you used to understand it?


    The man pages on Karmic - "man start" etc. and the documentation on the
    upstart website: <http://upstart.ubuntu.com/> - check the getting
    started page and the wiki. Also some blog pages by its author:
    <http://www.netsplit.com/category/tech/upstart/>
     
    Paul Rudin, Nov 14, 2009
    #2
    1. Advertising

  3. Paul Rudin

    Paul Rudin Guest

    Re: python-daemon and upstart

    Paul Rudin <> writes:

    > Ben Finney <> writes:
    >
    >> Paul Rudin <> writes:


    >>> description "test daemon"
    >>> expect daemon
    >>> chdir /tmp
    >>> exec /tmp/testdaemon.py


    Further experimentation reveals that by omitting the "expect daemon"
    stanza everything works fine. But I'm still a confused - the manpage
    says:


    expect daemon
    Specifies that the job's main process is a daemon, and will fork
    twice after being run. init(8) will follow this daemonisation,
    and will wait for this to occur before running the job's post-
    start script or considering the job to be running.

    Without this stanza init(8) is unable to supervise daemon proâ€
    cesses and will believe them to have stopped as soon as they
    daemonise on startup.


    So I would have expected it to be necessary in this case. Maybe this is
    more an upstart issue than a python-daemon one - not sure.
     
    Paul Rudin, Nov 14, 2009
    #3
  4. Paul Rudin

    Paul Rudin Guest

    Re: python-daemon and upstart

    Paul Rudin <> writes:

    >
    > So I would have expected it to be necessary in this case. Maybe this is
    > more an upstart issue than a python-daemon one - not sure.


    Aha - so I discover that if detach_process is not explicitly passed to
    the DaemonContext initialiser it tries to guess whether it needs to do
    the double fork thing or not, passing it through explicitly makes every
    work as I expected.

    So it's probably an rtfm error on my part. Although I suppose that there
    may be an argument the the DaemonContext guessing should try and detect
    this situation.... not sure.
     
    Paul Rudin, Nov 14, 2009
    #4
    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. babbu Pehlwan

    How to daemonize a HTTPServer

    babbu Pehlwan, Sep 1, 2011, in forum: Python
    Replies:
    1
    Views:
    203
  2. Payton Swick

    Daemonize like fetchmail

    Payton Swick, Jan 5, 2006, in forum: Ruby
    Replies:
    7
    Views:
    116
    tony summerfelt
    Jan 11, 2006
  3. Rogue Amateur
    Replies:
    6
    Views:
    107
    Rogue Amateur
    Jul 14, 2006
  4. Guido De Rosa
    Replies:
    1
    Views:
    134
    Brian Candler
    Mar 9, 2010
  5. Chris Angelico

    Re: [GENERAL] Postgres and Upstart

    Chris Angelico, Aug 10, 2012, in forum: Python
    Replies:
    0
    Views:
    119
    Chris Angelico
    Aug 10, 2012
Loading...

Share This Page