Using exec() to run external processes

Discussion in 'Java' started by John English, Oct 24, 2005.

  1. John English

    John English Guest

    I have a couple of problems using exec():

    1) I'm running a program which produces output to both standard
    output and standard error streams. How can I read from one or
    the other, depending on which has been written to? At the moment
    I read from the standard output stream, but this blocks if there
    is nothing to be read and I never get a chance to read from the
    standard error stream. As a workaround I use "2>&1" to combine
    the streams, but might not always be able to do so easily (e.g.
    if I want to redirect the standard output to a file).

    2) I have a separate thread which calls destroy() on the Process
    after 10 seconds to limit maximum execution time. If I have a
    program which runs for 50 seconds, it calls destroy() correctly
    after 10 seconds, but the call to waitFor() doesn't complete
    until the full 50 seconds have elapsed, and then bad things
    start happening (e.g. a database connection gets dropped for
    no apparent reason). This is on Windows XP, which may be part
    of the problem... :)

    Any ideas?

    TIA,

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
    John English, Oct 24, 2005
    #1
    1. Advertising

  2. John English

    Roedy Green Guest

    On Mon, 24 Oct 2005 13:06:27 +0100, John English <>
    wrote, quoted or indirectly quoted someone who said :

    >1) I'm running a program which produces output to both standard
    > output and standard error streams. How can I read from one or
    > the other, depending on which has been written to? At the moment
    > I read from the standard output stream, but this blocks if there
    > is nothing to be read and I never get a chance to read from the
    > standard error stream. As a workaround I use "2>&1" to combine
    > the streams, but might not always be able to do so easily (e.g.
    > if I want to redirect the standard output to a file).


    Use separate three threads to write, and read each of the err and
    output streams.

    See http://mindprod.com/jgloss/exec.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 24, 2005
    #2
    1. Advertising

  3. John English

    Roedy Green Guest

    On Mon, 24 Oct 2005 13:06:27 +0100, John English <>
    wrote, quoted or indirectly quoted someone who said :

    >2) I have a separate thread which calls destroy() on the Process
    > after 10 seconds to limit maximum execution time. If I have a
    > program which runs for 50 seconds, it calls destroy() correctly
    > after 10 seconds, but the call to waitFor() doesn't complete
    > until the full 50 seconds have elapsed,


    If you want the waitfor() to terminate early, you must call
    thatThread.interrurupt() from some other thread.

    waitfor() is sitting there waiting for either you or the child
    spawned's task return to kick it off again.

    see http://mindprod.com/jgloss/exec.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 24, 2005
    #3
  4. John English

    John English Guest

    Roedy Green wrote:

    > On Mon, 24 Oct 2005 13:06:27 +0100, John English <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >
    >>2) I have a separate thread which calls destroy() on the Process
    >> after 10 seconds to limit maximum execution time. If I have a
    >> program which runs for 50 seconds, it calls destroy() correctly
    >> after 10 seconds, but the call to waitFor() doesn't complete
    >> until the full 50 seconds have elapsed,

    >
    >
    > If you want the waitfor() to terminate early, you must call
    > thatThread.interrurupt() from some other thread.


    I seem to be doing that. My watchdog thread does this:

    try {
    sleep(timeout);
    process.destroy();
    parentThread.interrupt();
    }
    catch (InterruptedException e) { }

    and the parent thread does this:

    Process p = runtime.exec(command);
    BufferedReader r = new BufferedReader(
    new InputStreamReader(p.getInputStream())
    );
    int x = -1;
    String s = null;
    Watchdog w = new Watchdog(p,Thread.currentThread(),timeout);
    w.start();
    try {
    while ((s = r.readLine()) != null) {
    ...
    }
    p.waitFor();
    x = p.exitValue();
    w.interrupt();
    }
    catch (InterruptedException e) {
    ...
    }

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
    John English, Oct 25, 2005
    #4
  5. John English

    John English Guest

    Roedy Green wrote:

    > On Mon, 24 Oct 2005 13:06:27 +0100, John English <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >
    >>1) I'm running a program which produces output to both standard
    >> output and standard error streams. How can I read from one or
    >> the other, depending on which has been written to? At the moment
    >> I read from the standard output stream, but this blocks if there
    >> is nothing to be read and I never get a chance to read from the
    >> standard error stream. As a workaround I use "2>&1" to combine
    >> the streams, but might not always be able to do so easily (e.g.
    >> if I want to redirect the standard output to a file).

    >
    >
    > Use separate three threads to write, and read each of the err and
    > output streams.


    Ah, good idea. Thanks.

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
    John English, Oct 25, 2005
    #5
  6. John English

    Dom Gilligan Guest

    John - burks.bton.ac.uk seems to have been offline for the past few
    weeks; are you aware of this? Is it mirrored anywhere else?

    Thanks -

    Dom
    Dom Gilligan, Oct 25, 2005
    #6
  7. John English

    John English Guest

    Dom Gilligan wrote:

    > John - burks.bton.ac.uk seems to have been offline for the past few
    > weeks; are you aware of this? Is it mirrored anywhere else?


    Ah, must change my .sig; the server has been decommissioned now.

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS |
    University of Brighton |
    -----------------------------------------------------------------
    John English, Oct 27, 2005
    #7
    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. bugbear
    Replies:
    1
    Views:
    1,020
    bugbear
    Nov 4, 2005
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,096
    Gordon Beaton
    May 22, 2006
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,093
    David Schwartz
    Dec 9, 2003
  4. Marc Heiler
    Replies:
    1
    Views:
    165
    Robert Klemme
    May 24, 2009
  5. Guillermo Riojas
    Replies:
    0
    Views:
    157
    Guillermo Riojas
    Nov 26, 2010
Loading...

Share This Page