fork/exec with input redirection

Discussion in 'Python' started by Dan Upton, Nov 26, 2007.

  1. Dan Upton

    Dan Upton Guest

    I have a Python script that does a fork/exec, so the parent process
    can get the child's PID and monitor /proc/PID/stat (on a CentOS
    system). Most of my processes' command lines are straightforward
    enough to do this with, but I have a handful that use < on the command
    line, eg

    ../gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst

    The only thing I could really think of to try was

    os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x86",
    "--quiet", "--mode", "gtp", "<", "13x13.tst"])

    but this apparently doesn't work. Is there some other way to
    accomplish what I'm going for?

    Thanks,
    -dan
    Dan Upton, Nov 26, 2007
    #1
    1. Advertising

  2. Dan Upton

    hdante Guest

    On Nov 26, 7:58 pm, "Dan Upton" <> wrote:
    > I have a Python script that does a fork/exec, so the parent process
    > can get the child's PID and monitor /proc/PID/stat (on a CentOS
    > system). Most of my processes' command lines are straightforward
    > enough to do this with, but I have a handful that use < on the command
    > line, eg
    >
    > ./gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst
    >
    > The only thing I could really think of to try was
    >
    > os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x86",
    > "--quiet", "--mode", "gtp", "<", "13x13.tst"])
    >
    > but this apparently doesn't work. Is there some other way to
    > accomplish what I'm going for?
    >
    > Thanks,
    > -dan


    IIRC,

    if os.fork() == 0:
    new_stdin = os.open('13x13.tst')
    os.dup2(new_stdin, sys.stdin.fileno())
    os.close(new_stdin)
    os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x886", "--
    quiet", "--mode", "gtp"])
    hdante, Nov 27, 2007
    #2
    1. Advertising

  3. Dan Upton

    hdante Guest

    On Nov 27, 12:09 am, hdante <> wrote:
    > On Nov 26, 7:58 pm, "Dan Upton" <> wrote:
    >
    >
    >
    > > I have a Python script that does a fork/exec, so the parent process
    > > can get the child's PID and monitor /proc/PID/stat (on a CentOS
    > > system). Most of my processes' command lines are straightforward
    > > enough to do this with, but I have a handful that use < on the command
    > > line, eg

    >
    > > ./gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst

    >
    > > The only thing I could really think of to try was

    >
    > > os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x86",
    > > "--quiet", "--mode", "gtp", "<", "13x13.tst"])

    >
    > > but this apparently doesn't work. Is there some other way to
    > > accomplish what I'm going for?

    >
    > > Thanks,
    > > -dan

    >
    > IIRC,
    >
    > if os.fork() == 0:
    > new_stdin = os.open('13x13.tst')
    > os.dup2(new_stdin, sys.stdin.fileno())
    > os.close(new_stdin)
    > os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x886", "--
    > quiet", "--mode", "gtp"])


    Maybe a sys.stdin.flush() just to be sure ?
    hdante, Nov 27, 2007
    #3
  4. Dan Upton

    Dan Upton Guest

    > > > ./gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst
    > >
    > > > The only thing I could really think of to try was

    > >
    > > > os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x86",
    > > > "--quiet", "--mode", "gtp", "<", "13x13.tst"])

    > >
    > > > but this apparently doesn't work. Is there some other way to
    > > > accomplish what I'm going for?

    > >
    > > > Thanks,
    > > > -dan

    > >
    > > IIRC,
    > >
    > > if os.fork() == 0:
    > > new_stdin = os.open('13x13.tst')
    > > os.dup2(new_stdin, sys.stdin.fileno())
    > > os.close(new_stdin)
    > > os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x886", "--
    > > quiet", "--mode", "gtp"])

    >
    > Maybe a sys.stdin.flush() just to be sure ?
    >


    Thanks, that did the trick (well, os.open('13x13.tst', os.O_RDONLY),
    but you know... close enough).
    Dan Upton, Nov 27, 2007
    #4
    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. Patrick
    Replies:
    1
    Views:
    520
  2. Hoegje
    Replies:
    2
    Views:
    22,536
    Gianni Mariani
    Dec 5, 2003
  3. Benoit Dejean

    fork, exec, and disown

    Benoit Dejean, Feb 8, 2004, in forum: Python
    Replies:
    4
    Views:
    952
    Benoit Dejean
    Feb 9, 2004
  4. Ajay Bakhshi
    Replies:
    0
    Views:
    405
    Ajay Bakhshi
    May 3, 2004
  5. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    571
    Eric Snow
    Jan 8, 2009
Loading...

Share This Page