By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

Can't spawn, or popen from daemon

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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))

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.