redirecting stdout and stderr to /dev/null

Jim Dodgen jim at dodgen.us
Sat May 7 14:40:56 EDT 2016


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 -------------------


results when  run

root at dev:/home/jim# python test.py
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.36 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.00 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=1.01 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=1.16 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=1.02 ms
^C
--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 1.002/1.114/1.361/0.136 ms
root at dev:/home/jim#

*Jim Dodgen*



More information about the Python-list mailing list