Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Jim Dodgen Newsgroups: comp.lang.python Subject: Re: redirecting stdout and stderr to /dev/null Date: Sat, 7 May 2016 16:54:01 -0700 Lines: 136 Sender: jim.dodgen@gmail.com Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 7URPPPXE9FsnUp9HTJ+KmQplelYBsRBCKfYsCA9rLVTw== 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; 'skip:[ 20': 0.03; 'sys': 0.05; 'elegant': 0.07; 'cc:addr:python-list': 0.09; "'w')": 0.09; '/dev/null': 0.09; '__init__': 0.09; 'arg': 0.09; 'c2pwrite': 0.09; 'cmd': 0.09; 'errread,': 0.09; 'nameerror:': 0.09; 'observation': 0.09; 'retcode': 0.09; 'stderr': 0.09; 'stdout': 0.09; 'sys.stderr': 0.09; 'sys.stdout': 0.09; 'python': 0.10; 'python.': 0.11; 'output': 0.13; 'weird': 0.15; '2016': 0.16; 'descriptors,': 0.16; 'descriptors.': 0.16; 'fds': 0.16; 'from:addr:jim': 0.16; 'googling': 0.16; 'hint': 0.16; 'luck,': 0.16; 'proc': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:> 20': 0.16; 'subject:dev': 0.16; 'subprocess': 0.16; 'to:addr:martin': 0.16; 'tracebacks': 0.16; 'ubuntu,': 0.16; 'versed': 0.16; '\xc2\xa0i': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'sender:addr:gmail.com': 0.18; 'attribute': 0.18; 'version.': 0.18; '>': 0.18; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'skip:" 30': 0.20; 'martin': 0.22; '"",': 0.22; '(by': 0.22; 'exec': 0.22; 'see:': 0.22; 'cc:no real name:2**0': 0.22; 'defined': 0.23; 'bit': 0.23; 'seems': 0.23; 'sat,': 0.23; 'this:': 0.23; 'second': 0.24; 'tried': 0.24; 'import': 0.24; '(most': 0.24; '(this': 0.24; 'unix': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'command': 0.26; 'earlier': 0.27; 'figure': 0.27; 'define': 0.27; 'message- id:@mail.gmail.com': 0.27; 'idea': 0.28; 'skip:( 20': 0.28; 'looks': 0.29; 'fork': 0.29; 'perl': 0.29; 'raise': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'compared': 0.30; 'position.': 0.30; 'skip:& 30': 0.30; 'skip:[ 10': 0.31; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'run': 0.33; 'url:python': 0.33; '8bit%:25': 0.33; 'adjust': 0.33; 'shorter': 0.33; 'traceback': 0.33; 'skip:- 10': 0.34; 'languages': 0.34; "skip:' 20": 0.34; 'file': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'so,': 0.35; 'next': 0.35; 'skip:> 10': 0.35; 'quite': 0.35; 'but': 0.36; 'should': 0.36; 'needed': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'url:library': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'expect': 0.37; 'method': 0.37; 'there,': 0.37; 'thanks': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'doing': 0.38; 'received:209': 0.38; 'names': 0.38; 'skip:p 20': 0.38; 'test': 0.39; 'does': 0.39; 'rather': 0.39; 'some': 0.40; 'url:3': 0.60; 'care': 0.60; 'your': 0.60; "you'll": 0.61; 'hope': 0.61; 'more': 0.63; 'different': 0.63; 'here': 0.66; 'cut': 0.67; 'repeat': 0.67; 'child,': 0.84; 'isolate': 0.84; 'redirecting': 0.84; '*thanks': 0.91; 'mean.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=c6kKPeDp3p9IcaLwCx/FiF1Fugjqim4VPavNBSvUM5s=; b=Ut8fAbgw9ygiCASl8WzWouu622yKdF+2ELekTbECzry/lpLv7DAO13z8HLRfOJQzzo GkoUd+76tcoLVV18jsIf/ind/YSaEqMv95zUBLWf9/xFCyNUzB9+/6KHN97PtVY5nL1w SI0n92Pwb+Qxn3Rw3/+AwGjYlwUTQJkHBShTxeMxk7RxJ0xBPHkhB+MEQ9Sn08Ql1Hba Uv8QOuF3skzTUrNXK/AABM429UDgBjSRV8JQqjDrNvB81IqakbeoQq3KrKohaddo18x5 a09Gp0hMU7hpnBKbgNyAnm4z/xLwhcKZjk3mty5bMP97gh0kd5Kp/Eeghb8RwpXSTJSm LVqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=c6kKPeDp3p9IcaLwCx/FiF1Fugjqim4VPavNBSvUM5s=; b=E70dxdiDZ1Yo7VdtfZff9ZHdj9a2166Cw3DMzXTmQgnzSozvCpNN+Pf5FB71S7uh7R yP7e1/UzXae6mThwThRgxKXl+mGYNWzvNM1KYe4UW8bcwAaLjkfTt+WCw1dIbVroOBJ2 oi/uHLO3CS+CX/ttffE0fA0pkCOm8l+NHUf3hv/JzX9TpNzPnC9LO7PRBN5AGiYL2vke sk5mIT1L7j2lF2h+Fw3aP7aPJm9uH0i3vwg4aRtXCXQEekKKx+1lZ2se8hZICoSMuQ+I VauzzuizB5jmXDbR7yodwrgvAm7KJgt6mJ+2Qbav40SrBvoAkVHYsxrFSvcdDoL8qRUb E1rw== X-Gm-Message-State: AOPr4FXPcBVbtH6tIbClMVa2Q42dnqmOVKBBSZvlWAq3M0YlTh3s1JboTd4/Wtg+sbB8E6cCvFg0MW/ydefG0Q== X-Received: by 10.176.2.75 with SMTP id 69mr16988761uas.104.1462665281214; Sat, 07 May 2016 16:54:41 -0700 (PDT) In-Reply-To: X-Google-Sender-Auth: 3Omom9CMRX1fHOgN6LkUhx3TJQk X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:108308 *Thanks for the help* On Sat, May 7, 2016 at 12:16 PM, Martin A. Brown wrote: > > Hello there, > > >I'm new to python but well versed on other languages such as C and > >Perl > > > >I'm have problems redirecting stdout and stderr to /dev/null in a > >program that does a fork and exec. T found this method googling > >around and it is quite elegant compared to to the Perl version. > > > >So to isolate things I made a much shorter test program and it > >still is not redirecting. What am I doing wrong? > > > >test program test.py > >----------------- cut here ------------------- > >import sys > >import os > > > >f = open(os.devnull, 'w') > >sys.stdout = f > >sys.stderr = f > >os.execl("/bin/ping", "", "-w", "20", "192.168.1.1"); > >------------------ cut here ------------------- > > Think about the file descriptors. > > Unix doesn't care what the name is, rather that the process inherits > the FDs from the parent. So, your solution might need to be a bit > more complicated to achieve what you desire. Run the following to > see what I mean. > > realstdout = sys.stdout > realstderr = sys.stderr > f = open(os.devnull, 'w') > sys.stdout = f > sys.stderr = f > > print("realstdout FD: %d" % (realstdout.fileno(),), file=realstdout) > print("realstderr FD: %d" % (realstderr.fileno(),), file=realstdout) > print("sys.stdout FD: %d" % (sys.stdout.fileno(),), file=realstdout) > print("sys.stderr FD: %d" % (sys.stderr.fileno(),), file=realstdout) > > That should produce output that looks like this: > > realstdout FD: 1 > realstderr FD: 2 > sys.stdout FD: 3 > sys.stderr FD: 3 > > I hope that's a good hint... > > I like the idea of simply calling the next program using one of the > exec() variants, but you'll have to adjust the file descriptors, > rather than just the names used by Python. > > If you don't need to exec(), but just run a child, then here's the > next hint (this is for Python 3.5): > > import subprocess > cmd = ["ping", "-w", "20", "192.168.1.1"] > devnull = subprocess.DEVNULL > proc = subprocess.run(cmd, stdout=devnull, stderr=devnull) > proc.check_returncode() > > (By the way, your "ping" command looked like it had an empty token > in the second arg position. Looked weird to me, so I removed it > in my examples.) > The empty token is needed but useless, it is arg[0] most people just repeat the program name > > For subprocess.run, see: > > https://docs.python.org/3/library/subprocess.html#subprocess.run > > For earlier Python versions without run(), you can use Popen(): > > import subprocess > cmd = ["/bin/ping", "-w", "20", "192.168.1.1"] > devnull = subprocess.DEVNULL > proc = subprocess.Popen(cmd, stdout=devnull, stderr=devnull) > retcode = proc.wait() > if retcode != 0: > raise FlamingHorribleDeath > > You will have to define FlamingHorribleDeath or figure out what you > want to do in the event of the various different types of > failure....if you don't then, you'll just see this: > > NameError: name 'FlamingHorribleDeath' is not defined > > Good luck, > > -Martin > > -- > Martin A. Brown > http://linux-ip.net/ seems not to find subprocess.DEVNULL I'm on 2.7.6 Ubuntu, and 2.7.9 on Raspbian I expect I need 3.5 here are some Tracebacks I get, the second one is when I tried os.devnull in place of subprocess.DEVNULL Traceback (most recent call last): File "test.py", line 5, in devnull = subprocess.DEVNULL AttributeError: 'module' object has no attribute 'DEVNULL' Traceback (most recent call last): File "test.py", line 6, in proc = subprocess.Popen(cmd, stdout=os.devnull, stderr=os.devnull) File "/usr/lib/python2.7/subprocess.py", line 702, in __init__ errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr) File "/usr/lib/python2.7/subprocess.py", line 1128, in _get_handles c2pwrite = stdout.fileno() AttributeError: 'str' object has no attribute 'fileno' One other observation it looks as Popen behaves the same way as my fork exec would