python-daemonize and upstart

P

Paul Rudin

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...
 
P

Paul Rudin

Ben Finney said:
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

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.
Good to know.


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/>
 
P

Paul Rudin

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.
 
P

Paul Rudin

Paul Rudin said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top