execl() and inheritied streams

Discussion in 'Python' started by Erik Johnson, Sep 28, 2004.

  1. Erik Johnson

    Erik Johnson Guest

    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
    Erik Johnson, Sep 28, 2004
    #1
    1. Advertising

  2. Erik Johnson

    Donn Cave Guest

    In article <4159b1d6$>,
    "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 Cave, Sep 28, 2004
    #2
    1. Advertising

  3. Erik Johnson

    Erik Johnson Guest

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

    -ej

    "Donn Cave" <> wrote in message
    news:...

    > 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(...
    Erik Johnson, Sep 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC

    pipe() dup2() fork() execl() select()

    PC, Aug 9, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,239
  2. python newbie

    execl difficulty

    python newbie, Dec 14, 2003, in forum: Python
    Replies:
    9
    Views:
    5,104
  3. Chris Green

    Popen3-style os.execl?

    Chris Green, May 18, 2004, in forum: Python
    Replies:
    4
    Views:
    329
    Michael Hudson
    May 19, 2004
  4. shalombi

    Execl() in windows

    shalombi, Sep 23, 2005, in forum: C Programming
    Replies:
    1
    Views:
    701
    Keith Thompson
    Sep 24, 2005
  5. kisshug

    execl function

    kisshug, Dec 25, 2005, in forum: C Programming
    Replies:
    1
    Views:
    406
    Jack Klein
    Dec 25, 2005
Loading...

Share This Page