execl() and inheritied streams
Donn Cave
donn at u.washington.edu
Tue Sep 28 16:08:04 EDT 2004
In article <4159b1d6$1 at nntp.zianet.com>,
"Erik Johnson" <ej <at> wellkeeper <dot> com> wrote:
> 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.
sys.stdin etc. are objects that live in the Python interpreter.
Basically, they're a buffer plus a file descriptor, plus some
functions. Similar to the C stdio FILE object, which in fact
is underneath the Python fileobject.
Objects in interpreter memory space do not survive an exec.
You want to remap the standard UNIX file descriptors 0 (input),
1 (output) and 2 (error). They are the system level I/O streams
that will survive exec.
Like,
fd = os.open(filename, os.O_WRONLY|os.O_CREAT)
os.dup2(fd, 1)
os.dup2(fd, 2)
os.close(fd)
os.execve(...
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list