Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34615

Re: forking and avoiding zombies!

Date 2012-12-11 09:33 -0300
From peter <pjmakey2@gmail.com>
Subject Re: forking and avoiding zombies!
References <CAF_E5JbjkWfjyCg3HwuQLekzqm=8c7X9iR=D0rZF+ekr4WcEPw@mail.gmail.com> <50C6153F.8050205@gmail.com> <CAF_E5JbPa9gb_a7o1ZgwaNfhu_ycz+CdnMCvhDe2VC8Dy5WAgA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.723.1355229715.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 12/11/2012 08:47 AM, andrea crotti wrote:
> Yes I wanted to avoid to do something too complex, anyway I'll just
> comment it well and add a link to the original code..
>
> But this is now failing to me:
>
> def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
>      # Perform first fork.
>      try:
>          pid = os.fork()
>          if pid > 0:
>              sys.exit(0) # Exit first parent.
>      except OSError as e:
>          sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
>          sys.exit(1)
>
>      # Decouple from parent environment.
>      os.chdir("/")
>      os.umask(0)
>      os.setsid()
>
>      # Perform second fork.
>      try:
>          pid = os.fork()
>          if pid > 0:
>              sys.exit(0) # Exit second parent.
>      except OSError, e:
>          sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
>          sys.exit(1)
>
>      # The process is now daemonized, redirect standard file descriptors.
>      sys.stdout.flush()
>      sys.stderr.flush()
>
>      si = file(stdin, 'r')
>      so = file(stdout, 'a+')
>      se = file(stderr, 'a+', 0)
>      os.dup2(si.fileno(), sys.stdin.fileno())
>      os.dup2(so.fileno(), sys.stdout.fileno())
>      os.dup2(se.fileno(), sys.stderr.fileno())
>
>
> if __name__ == '__main__':
>      daemonize(stdout='sample_file', stderr='sample')
>      print("hello world, now should be the child!")
>
>
> [andrea@andreacrotti experiments]$ python2 daemon.py
> Traceback (most recent call last):
>    File "daemon.py", line 49, in <module>
>      daemonize(stdout='sample_file', stderr='sample')
>    File "daemon.py", line 41, in daemonize
>      so = file(stdout, 'a+')
> IOError: [Errno 13] Permission denied: 'sample_file'
>
> The parent process can write to that file easily, but the child can't,
> why is it working for you and not for me though?
> (Running this on Linux with a non-root user)
In the time when you fork the proccess you can't use relative path, is 
dangerous. You need to use absolute path's like this.

import os, sys

def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
     # Perform first fork.
     try:
         pid = os.fork()
         if pid > 0:
             sys.exit(0) # Exit first parent.
     except OSError as e:
         sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, 
e.strerror))
         sys.exit(1)

     # Decouple from parent environment.
     os.chdir("/")
     os.umask(0)
     os.setsid()

     # Perform second fork.
     try:
         pid = os.fork()
         if pid > 0:
             sys.exit(0) # Exit second parent.
     except OSError, e:
         sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, 
e.strerror))
         sys.exit(1)

     # The process is now daemonized, redirect standard file descriptors.
     sys.stdout.flush()
     sys.stderr.flush()

     si = file(stdin, 'r')
     so = file(stdout, 'a+')
     se = file(stderr, 'a+', 0)
     os.dup2(si.fileno(), sys.stdin.fileno())
     os.dup2(so.fileno(), sys.stdout.fileno())
     os.dup2(se.fileno(), sys.stderr.fileno())


if __name__ == '__main__':
     daemonize(stdout='/tmp/sample_file.log', stderr='/tmp/sample.log')
     print("hello world, now should be the child!")

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: forking and avoiding zombies! peter <pjmakey2@gmail.com> - 2012-12-11 09:33 -0300

csiph-web