Runtime.getRuntime().exec(...) -> Java process hangs in STOP state
Thomas Lange
freebja at wie-ich.de
Sun Mar 6 10:40:37 GMT 2005
Hi everyone,
has anybody experienced a similar problem?
I'm running tomcat with an apache / mod_jk frontend.
After running a few days under medium load, a call of
Runtime.getRuntime().exec(...) will not return. Also the Java process will
hang infinitely in a STOP state (as displayed by top).
As a result, mbufs or other resources will fill up until the entire server
runs out of resources and hangs (creation of new processes is not possible
anymore).
I'm using FreeBSD 5.3-RELEASE-p5, jdk1.4, patchset 7. Everything (including
the kernel) is compiled with -O2.
Checking the output of my programm shows, that all processes created by it
terminate correctly (that is, all but the last).
In an ealier version (FreeBSD 4.9-RELEASE-p4), jdk1.4, patchset 6 there is a
similar problem. Here calling Runtime.getRuntime().exec(...) results in
creating a very high CPU load and not returning from the exec() call.
My applications calls the exec()-method once every few minutes.
Might it even be a threading issue?
I've included a snippet from my program as an attachment.
Any comments or suggestions are welcome!
Regards
Thomas
public static boolean executeCommand(String exCommand, int exTimeout) {
String command = "nice -n 15 ";
command = command + exCommand;
Process process = null;
try {
System.out.println("Executing external command " + command);
process = Runtime.getRuntime().exec(command);
}
catch (IOException e) {
System.out.println(e.toString());
return false;
}
InputStream inputStream = process.getInputStream();
BufferedInputStream bufferedInputStream = new
BufferedInputStream(inputStream);
InputStream errorStream = process.getErrorStream();
BufferedInputStream bufferedErrorStream = new
BufferedInputStream(errorStream);
boolean ok = false;
int timeout = exTimeout;
int exitValue = -999;
while (!ok) {
try {
while (bufferedInputStream.available() > 0 ||
bufferedErrorStream.available() > 0) {
while (bufferedInputStream.available() > 0) {
System.out.print((char)bufferedInputStream.read());
}
while (bufferedErrorStream.available() > 0) {
System.out.print((char)bufferedErrorStream.read());
}
}
}
catch (IOException e) {
System.out.println("Couldn't read response");
}
try {
exitValue = process.exitValue();
ok = true;
}
catch (IllegalThreadStateException e) {
try {
// still running.
Thread.sleep(300);
timeout = timeout - 300;
if (timeout < 0 && timeout >= -300) {
System.out.println("ALERT: Command doesn't terminate:");
System.out.println(command);
System.out.println("Shutting down command...");
process.destroy();
}
else if (timeout <0) {
System.out.println("ALERT: Command STILL doesn't
terminate:");
System.out.println(command);
Thread.sleep(1000);
}
} catch (InterruptedException e1) {
// doesn't matter
}
}
}
if (ok) {
// finished running
if (exitValue == 0) {
System.out.println("Terminated without errors");
}
else {
System.out.println("Exit code " + exitValue + " while performing
command " + command);
}
}
else {
process.destroy();
}
try {
while (bufferedInputStream.available() > 0) {
System.out.print((char)bufferedInputStream.read());
}
while (bufferedErrorStream.available() > 0) {
System.out.print((char)bufferedErrorStream.read());
}
}
catch (IOException e) {
System.out.println("Couldn't read response");
}
return exitValue == 0;
}
More information about the freebsd-java
mailing list