Can't spawn, or popen from daemon

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Roach

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

    ... cut 'n paste code ...

    #Additional closing of stdio

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

    # 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

  • Diez B. Roggisch

    #2
    Re: Can't spawn, or popen from daemon

    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

    Comment

    • Jeff Epler

      #3
      Re: Can't spawn, or popen from daemon

      Imagine that /usr/bin/lp has something like the following Python code at
      the top:
      sys.stderr.writ e("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

      Comment

      • Mark Roach

        #4
        Re: Can't spawn, or popen from daemon

        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



        Comment

        Working...