I am trying to execute a sh script file from a Java method.
The key part of the code looks like this:
Process p = null;
try {
p = Runtime.getRuntime().exec("start_team.sh");
} catch (IOException e) {
System.out.println(e.toString());
}
try {
(Thread.currentThread()).sleep(30000);
} catch (Exception e) {
System.out.println(e.toString());
}
The script runs fine on its own ("./start_team.sh" at command line),
but when we
execute this code, it freezes up about midway through, while the main
thread is still sleeping.
Does anyone know where to look to fix this code? Is there something
obvious I'm missing?
Thanks for your time.
Gordon Beaton - 14 Mar 2006 22:13 GMT
> I am trying to execute a sh script file from a Java method.
[...]
> The script runs fine on its own ("./start_team.sh" at command line),
> but when we execute this code, it freezes up about midway through,
> while the main thread is still sleeping.
You need to either read the output of the child process as it runs, or
prevent it from producing any output (for example, with redirection).
BTW Thread.sleep() is a static method, so calling sleep() with
currentThread() is bad style (and unnecessary).
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Oliver Wong - 14 Mar 2006 23:46 GMT
>I am trying to execute a sh script file from a Java method.
>
[quoted text clipped - 21 lines]
> Does anyone know where to look to fix this code? Is there something
> obvious I'm missing?
See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html
<quote>
Because some native platforms only provide limited buffer size for standard
input and output streams, failure to promptly write the input stream or read
the output stream of the subprocess may cause the subprocess to block, and
even deadlock.
</quote>
- Oliver
Thomas Nelson - 15 Mar 2006 01:11 GMT
Thanks to you both, I read the input stream into a buffer, and piped
the error stream away to /dev/null, and now the code seems to work.
THN