Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news.skynet.be!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'received:209.85.223': 0.03; 'skip:[ 20': 0.03; 'failing': 0.05; '__name__': 0.07; 'exit': 0.07; 'try:': 0.07; '%s\\n"': 0.09; '(%d)': 0.09; 'failed:': 0.09; 'ioerror:': 0.09; 'oserror': 0.09; 'cc:addr :python-list': 0.10; '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; 'pfxlen:0': 0.17; 'permission': 0.20; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'linux': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'environment.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'parent': 0.29; "skip:' 10": 0.30; 'file': 0.32; 'traceback': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'add': 0.36; 'except': 0.36; 'but': 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; 'header:Received:5': 0.40; 'link': 0.60; 'first': 0.61; '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=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=3U41V9+5ABWVNL6r+zLUcLT9rtNDHwHZ514mo/7jUFI=; b=Vap057QenFIkBbmZ64AxLFebKHNCCSwi7kxEjA0ZCAzo/MkQQ0XMq0JhVmwxRpNF6I j1m7htKuoi2LQg3QQ5nNRW9enzCdWq7WZRiWEQW0vCGQz93XG09a4YAGlkdK6X9vMeSy k17QW4PhfrS533Zpi6kXELE7kjLVxGTr+KJQh4rob52JpPUWOtzmsaZpIQQX4a+OSzEC ZZZGHbQoe0ZJLd+DmKJb0aZsalPCCkaQm45w6/7TP7wRRSvf9mtpkIUQQvT15kySFMwh 9hetvLNrBmseDSTkLt6IUadZDTvhnJKfjsac5MDv8/A/uk7UCqFBT0uST5VNKSmOF9Tl UV9w== MIME-Version: 1.0 In-Reply-To: <50C6153F.8050205@gmail.com> References: <50C6153F.8050205@gmail.com> Date: Tue, 11 Dec 2012 11:47:03 +0000 Subject: Re: forking and avoiding zombies! From: andrea crotti To: peter Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355226762 news.xs4all.nl 6955 [2001:888:2000:d::a6]:36896 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34613 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 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)