Can't spawn, or popen from daemon

Discussion in 'Python' started by Mark Roach, Apr 20, 2004.

  1. Mark Roach

    Mark Roach Guest

    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
    Mark Roach, Apr 20, 2004
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Apr 21, 2004
    #2
    1. Advertising

  3. Mark Roach

    Jeff Epler Guest

    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
    Jeff Epler, Apr 21, 2004
    #3
  4. Mark Roach

    Mark Roach Guest

    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
    Mark Roach, Apr 21, 2004
    #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. Derek Basch
    Replies:
    2
    Views:
    1,286
    Donn Cave
    Jan 21, 2005
  2. EEK
    Replies:
    1
    Views:
    324
    Gabriel Genellina
    Sep 30, 2008
  3. Ed Hames
    Replies:
    0
    Views:
    364
    Ed Hames
    Apr 16, 2008
  4. Edgardo Hames
    Replies:
    1
    Views:
    334
    Ed Hames
    May 6, 2008
  5. Jun Young Kim
    Replies:
    3
    Views:
    123
    Robert Klemme
    Apr 20, 2009
Loading...

Share This Page