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


Groups > comp.lang.python > #34615

Re: forking and avoiding zombies!

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <pjmakey2@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'failing': 0.05; 'sys': 0.05; '__name__': 0.07; 'exit': 0.07; 'try:': 0.07; '%s\\n"': 0.09; '(%d)': 0.09; 'failed:': 0.09; 'fork': 0.09; 'ioerror:': 0.09; 'oserror': 0.09; 'path,': 0.09; 'def': 0.10; 'anyway': 0.11; "'__main__':": 0.16; "'r')": 0.16; '49,': 0.16; 'descriptors.': 0.16; 'subject:avoiding': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'wrote:': 0.17; 'permission': 0.20; 'import': 0.21; 'os,': 0.22; 'absolute': 0.23; 'to:2**1': 0.23; 'second': 0.24; 'linux': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'am,': 0.27; 'environment.': 0.27; 'parent': 0.29; 'this.': 0.29; "skip:' 10": 0.30; 'relative': 0.30; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'add': 0.36; 'except': 0.36; 'but': 0.36; 'message- id:@gmail.com': 0.36; 'wanted': 0.36; 'child': 0.36; "i'll": 0.36; 'should': 0.36; 'too': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'comment': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'link': 0.60; 'first': 0.61; 'received:200': 0.71; 'andrea': 0.84; 'complex,': 0.84; 'user)': 0.84; '(running': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=aeEGJw1XinvNlSJ+sjZ61OY+aqJpsKJhOiB0+YNze5U=; b=CvAxjI5jhIr1pu8ZMK+WneejSinx9NbtE13d5uDJ5Ayn7hhLDKfaHXXndSUicrMEW1 MHfE5HxgvwQ4I5/zCev4MPEogqHZtGcNrnnsdDDH2ipADbUvqexfVPZwAwhc7RuIFuB1 i1vP0rGhikfVrD5SHrk2th/kM0xFBxF9w3xnMgKjxz+v76oMwTJ/OEFJ/bfQ0tMCYQfF C7DHUgL9J3gyfrBPJTL97FW7wRBEfavrJK4GI8Phdfq1BP0yuJLSZCecTr0xS2cerH7L mdYnGliBlk2rzKclfnGGHwRI1i3V9cXVojlDoRjQPsfTP6B0hQta/zkr2cv/FeV3vKo9 6I6w==
Date Tue, 11 Dec 2012 09:33:47 -0300
From peter <pjmakey2@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120824 Thunderbird/15.0
MIME-Version 1.0
To andrea crotti <andrea.crotti.0@gmail.com>, python-list@python.org
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>
In-Reply-To <CAF_E5JbPa9gb_a7o1ZgwaNfhu_ycz+CdnMCvhDe2VC8Dy5WAgA@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.723.1355229715.29569.python-list@python.org> (permalink)
Lines 104
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1355229715 news.xs4all.nl 6909 [2001:888:2000:d::a6]:45443
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34615

Show key headers only | 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