469,610 Members | 2,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Can't spawn, or popen from daemon

I am using the daemonize (double-fork) code from the cookbook, and have
bumped into a strange issue. This is what my program is doing:

def main():
... useful things ...
os.popen('/usr/bin/lp -d printer1 %s' % (filename))

def daemonize(func):
"""cookbook recipe from

... cut 'n paste code ...

#Additional closing of stdio


# this is pretty obscure!

if __name__ == '__main__':

The thing that's strange is that the call to lp never happens... if I
replace daemonize(main) with just main() it works as expected. Is there
some secret requirement on stdin/out for popen/spawn/system calls? I
have tried all three, and they all work when I don't double-fork, but
don't when I do. I can't figure this out at all, and it doesn't help
matters that the problem only occurs in a situation where it is very
hard to debug...



Jul 18 '05 #1
3 2777
Not sure why its not working for you, but I have a similar situation where I
use daemonize and I can spawn using popen2.popen3:

out, inn, err = popen2.popen3(_.crm_executable % (path, program))


Diez B. Roggisch
Jul 18 '05 #2
Imagine that /usr/bin/lp has something like the following Python code at
the top:
sys.stderr (stdio's stderr in C) in the called program is closed, so
this will return an error. Whatever the error was, lp may be trying to
print *that* to stderr, too. If there's some sort of failure, you're
not going to see the message.

You could try:
os.popen('/usr/bin/lp -d printer1 %s 2>&1' % (filename))
to capture both stdout and stderr, or you could use the popen2 module to
get multiple handles for I/O with the child, or you could try (in
nullfd = os.open("/dev/null", os.O_RDWR)
os.dup2(nullfd, 0)
os.dup2(nullfd, 1)
os.dup2(nullfd, 2)
to make the standard C files point at /dev/null (writes succeed, but the
data is discarded), in case it's an error reading stdin or writing to
stderr that is killing lp.


Jul 18 '05 #3
Thanks to both of you guys for your responses, it turned out to be a
simple programmer-error (no surprise).

The original, non-daemon version of my program could take input from
stdin or from argv[1] and I was trying to test using stdin, forgetting
somehow that I had explicitly closed it already... I amaze myself

Thanks again,


Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by James Gregory | last post: by
7 posts views Thread by Scott Chapman | last post: by
6 posts views Thread by Ladvánszky Károly | last post: by
7 posts views Thread by Michael Ransburg | last post: by
17 posts views Thread by krishnakant Mane | last post: by
2 posts views Thread by Dmitry Teslenko | last post: by
1 post views Thread by EEK | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.