Surjya Narayana Padhi Surjya Narayana Padhi - 1 year ago 79
Java Question

How to wait for multi-threaded shell script execution to finish called inside my web service?

I have a java restful service method which executes a using processBuilder. My script takes one input (example - /path/to-a/folder).
Inside the script something like this

-> execute a command which is multithreaded i.e parallel processing
-> echo "my message"

Now when call my script from a linux command line it executes fine. First all the threads running finishes and then some text output from threaded command execution shown on terminal and then echo my message is shown.

But when I call the same script from java using processBuilder, the last echo message comes immidiately and execution ends.

Following the way I call my script from java

ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash","/path/to/","/path/to/folder/data");
Process proc = processBuilder.start();
StringBuffer output = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null){
output.append(line + "\n");

System.out.println("### " + output);

I don't know whats happening, how to debug also.
Can someone enlighten me on how to get the same behaviour from shell script when run from terminal or from java processBuilder?

Answer Source

Use ProcessBuilder.redirectErrorStream(boolean redirectErrorStream) with argument true to merge the errors into output. Alternatively, you could also use the shell command syntax cmd 2>&1 to merge the error with output.

These are some of the cases why you may be immediately getting the output of the last echo statement (instead of the script taking time to run and return proper results):

  • Missing environment variables
  • The launched bash needs to source .bashrc or some such recource file
  • The launched bash may not be running in right directory (you can set this in ProcessBuilder)
  • The launched bash may not be finding some script/executable in its PATH
  • The launched bash may not be finding proper libraries in the path for any of the executables

Once you merge error, you would be able to debug and see the errors for yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download