473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

execl() and inheritied streams

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@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@sand:~/src/python> cat print123
#! /usr/bin/python
import sys
print "one"
print "two"
print "three"
sys.stdout.flush()
ej@sand:~/src/python> rm out.txt
ej@sand:~/src/python> ./exec.py
one
two
three
ej@sand:~/src/python> cat out.txt
about to call exec...
ej@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
Jul 18 '05 #1
2 2374
In article <41********@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, do**@u.washington.edu
Jul 18 '05 #2

Thanks for your kind and helpful reply, Donn! :)

-ej

"Donn Cave" <do**@u.washington.edu> wrote in message
news:do************************@gnus01.u.washingto n.edu...
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(...

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: python newbie | last post by:
Can anyone tell me why this would cause "Invalid Number of Parameters" while trying to compile (or interpret or whatever) import os...
4
by: Chris Green | last post by:
Hey folks, I'm whipping up a quick CGI to wrap a command line app and I was a bit suprised to not find a execl-like Popen3 style object. I don't want to bother with the shell interpreting...
3
by: PC | last post by:
Is it possible to create pipe()s (stdin/stdout) for a child process and forking a shell with execl() and controlling the shell's stdin/stdout from the parent with select()? Heres a little snippet...
1
by: shalombi | last post by:
I am having a weird result with the execl() func under windows (i know its meant for unix). When I put the execl in a piece of code then run the app and when I give a value to execl all it does is...
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
1
by: kisshug | last post by:
i 'm working on a project in that i have 2 use 'execl' function can any one give how to use this function with 2 small programs in unix environment. -- kisshug Message posted via ...
1
by: antoniosk | last post by:
I have a problem calling a compiled program (for example hello.exe which stands for hello world) from another program (let's say time.exe) with execl function /* program time.c */ #include...
0
by: vishalgupta | last post by:
while working on vb we can work with acess and sql as backend but can we work with execl in backend ......is it efficient and easier to work with execl in backend or access in backend and why??
4
by: Sam | last post by:
I have a program which works great when run from the command line. But when I run it combined with something else such as: - piping it through less - cron - execl (i.e. calling it from another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.