Can't spawn, or popen from daemon

M

Mark Roach

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
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012"""

... cut 'n paste code ...

#Additional closing of stdio

sys.stdin.close()
sys.stdout.close()
sys.stderr.close()

# this is pretty obscure!
os.close(0)
os.close(1)
os.close(2)

if __name__ == '__main__':
daemonize(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...

Thanks,

Mark
 
D

Diez B. Roggisch

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))
 
J

Jeff Epler

Imagine that /usr/bin/lp has something like the following Python code at
the top:
sys.stderr.write("something\n")
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
daemonize):
nullfd = os.open("/dev/null", os.O_RDWR)
os.dup2(nullfd, 0)
os.dup2(nullfd, 1)
os.dup2(nullfd, 2)
os.close(nullfd)
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.

Jeff
 
M

Mark Roach

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

Thanks again,

Mark
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top