execl() and inheritied streams

Erik Johnson ej
Tue Sep 28 20:53:18 CEST 2004


Hi,

    I am trying to use Python to remap stdout and stderr to a log file for
an exec()'ed program.
The following code seems to work fine for the script itself, but my
expectation was that the exec'ed program would inherit its STDIN and STDOUT
and so would be printing to the log file also. Unfortunately, that is not
the case: the print statements in the spawned process come back to the
screen.  So... I'm a little puzzled and am not finding information on how to
remap the file descriptors other than to set sys.stdin, sys.stdout,
sys.stderr.

Here's my test:


ej at sand:~/src/python> cat exec.py
#! /usr/bin/python

import os, sys

fd = file('out.txt', 'a')
sys.stdout = fd
sys.stderr = fd

print "about to call exec..." # end up in out.txt as expected
sys.stdout.flush()            # provided you do this (otherwise it won't)

os.execl('print123', 'ignored')
ej at sand:~/src/python> cat print123
#! /usr/bin/python
import sys
print "one"
print "two"
print "three"
sys.stdout.flush()
ej at sand:~/src/python> rm out.txt
ej at sand:~/src/python> ./exec.py
one
two
three
ej at sand:~/src/python> cat out.txt
about to call exec...
ej at sand:~/src/python>


    So... can someone explain how to permenently change the file descriptors
that get inherited or otherwise explain what I'm doing wrong?  Thanks for
taking the time to read my post. :)

-ej





More information about the Python-list mailing list