473,560 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.flus h() # provided you do this (otherwise it won't)

os.execl('print 123', 'ignored')
ej@sand:~/src/python> cat print123
#! /usr/bin/python
import sys
print "one"
print "two"
print "three"
sys.stdout.flus h()
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 2384
In article <41********@nnt p.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(filenam e, os.O_WRONLY|os. O_CREAT)
os.dup2(fd, 1)
os.dup2(fd, 2)
os.close(fd)

os.execve(...

Donn Cave, do**@u.washingt on.edu
Jul 18 '05 #2

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

-ej

"Donn Cave" <do**@u.washing ton.edu> wrote in message
news:do******** *************** *@gnus01.u.wash ington.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(filenam e, 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
3019
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 os.execl("C:/WINDOWS/system32/xcopy.exe","E:/MainWeb/dreampics/*.*","E:/Main Web/dp") Here's what the Python reference says: execl(path,arg0, arg1, ...)
4
1804
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 metacharaters by constructing a command line. Basically, I want something that acts just like Popen3 but allows me to specify cmd as a path + args. ...
3
17377
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 of code. it doesnt actually work.. but it is how I envisioned it:: if((n=accept(s,(struct sockaddr *)&remote,&sz))==-1) {printf("accept\n");...
1
6157
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 rerun the prog or rerun just main() I dont know which. Is this normal? Is it ok to use this if i want to loop my app? I tried looking it up on...
1
2410
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 Stream, etc.). It then goes on to discuss the restriction that streams only read bytes or byte arrays that the .net framework provides a StreamReader...
1
3406
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 http://www.exforsys.com for all your training needs.
1
4385
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 <stdio.h> #include <unistd.h> main (int argc, char *argv) {
0
907
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
2413
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 python program) it gives me a unicode error File "../myparser.py", line 261, in set_attributes
0
7632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7552
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7841
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6191
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5458
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5175
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.