Using exec() to run external processes

J

John English

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:[email protected]
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
-----------------------------------------------------------------
 
R

Roedy Green

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
 
R

Roedy Green

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
 
J

John English

Roedy said:
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:[email protected]
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
-----------------------------------------------------------------
 
J

John English

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

Ah, good idea. Thanks.

-----------------------------------------------------------------
John English | mailto:[email protected]
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
-----------------------------------------------------------------
 
D

Dom Gilligan

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
 
J

John English

Dom said:
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:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS |
University of Brighton |
-----------------------------------------------------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top