0

I have this script below (called compute.sh and used to start ROOT application(CERN)(root.exe) and execute some c++ macros with it)

#!/bin/bash
ROOTSH=/path/to/files root -b -l macro.cpp
$ROOTSH &
ROOT_PID=$!
echo $ROOT_PID > .lock

In order to launch this .sh from my java deployed application on the same Linux server I use:

try{  
    Process p = Runtime.getRuntime().exec("/path/to/files/compute.sh");
    p.waitFor();
}catch( IOException ex ){
    log.warn("Cannot find bat or sh to start ROOT");
}catch( InterruptedException ex ){     
    log.warn("The ROOT execution is interrupted");
}

But when I try to start this .sh from th UI, I have nothing, no results and no errors. And when i run the .sh from outside the java code, it works perfectly.

Does anyone have an idea how to handle this please? Or Is there another way to make sure the shell script will be run from the java code?

hiba hawes
  • 61
  • 5
  • 1
    Debug your code. If it never reaches a `catch` statement nor the rest of the method, the script is running indefinitely (maybe for memory-related reasons). Also the preferred methodology is to use [ProcessBuilder.start](https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#start%28%29). – Mena Aug 16 '16 at 15:17
  • Also, check if the working directory is the right one. The bash script doesn't set it, and neither does your Java code. – Siguza Aug 16 '16 at 15:19
  • @Mena : I tried with ProcessBuilder.start, this does not solve the problem in my case. btw, I installed the .war of my application on tomcat in Linux server. when I debugged in local (my windows machine) with this .bat equivalent to this .sh, it worked well. Do you have an idea how to track this from the app package from tomcat on the linux server please? – hiba hawes Aug 16 '16 at 15:40
  • @Siguza the working directory I use is what I mentioned with: /path/to/files – hiba hawes Aug 16 '16 at 15:42
  • @hibahawes if you mean how to debug it, you can set up a socket and remote-debug from your IDE. – Mena Aug 16 '16 at 15:50

1 Answers1

0

Your compute.sh script could be outputting errors without your knowledge. You should capture the output and error streams. The following post should help with that: ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread

Community
  • 1
  • 1
eighthrazz
  • 331
  • 1
  • 6
  • Hi All, sorry for my late response. I solved the problem this way: Shell `#!/bin/bash export ROOTSH=/home/path_to_files cd $ROOTSH root.exe -b -l -q my_macro.cpp ROOT_PID=$! echo $ROOT_PID > .lock` Java: `Process p = new ProcessBuilder().inheritIO().command("the .sh file complete path").start(); p.waitFor();` Thank you all for help. – hiba hawes Sep 06 '16 at 10:13