Reputation: 111
I'm trying to use ProcessBuilder to execute a shell script on my Linux server, from a Servlet running on WebSphere Application Server.
The code returns 0 (using .waitFor()), but the script doesn't appear to execute. If I put an invalid path to the script I get a "file not found" exception, so I know it's finding the script...but doesn't appear to execute.
The script itself calls another script that should eventually output a zip file (i've also got a 'touch' line to see if anything's happening in there...but nothing doing).
The script runs fine from command line, using same command as I'm passing with .start().
Here's a snippet from my Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("in doPost");
System.out.println("about to kick off ProcessBuilder");
ProcessBuilder pb = new ProcessBuilder("/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/localhostNode01Cell/Svc_war.ear/Svc.war/test.sh");
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
int ch;
while ((ch = br.read()) != -1)
System.out.println((char)ch);
br.close();
try {
int exitVal = process.waitFor();
System.out.println("Exit Value: " + exitVal);
} catch (InterruptedException e) {
e.printStackTrace();
}
The .redirectErrorStream() and .getInputStream() were to see if I might be hitting some Buffer issues I've read others refer to (although I wouldn't expect the .waitFor() to return 0 if that were the case).
This is my first foray into ProcessBuilder, so I'm hoping I'm just missing something obvious.
Any ideas/hints would be appreciated.
Oh yeah...here's the output I get:
[4/5/13 21:32:41:791 PDT] 0000004d SystemOut O in doPost
[4/5/13 21:32:41:791 PDT] 0000004d SystemOut O about to kick off ProcessBuilder
[4/5/13 21:32:41:818 PDT] 0000004d SystemOut O Exit Value: 0
Thanks.
Upvotes: 2
Views: 2442
Reputation: 97
You simply need to put shell script in working directory. On doing so you don't need to set directory of ProcessBuilder by doing pb.directory("directory")
Upvotes: 2
Reputation: 111
Got it working....I needed to set the working directory using:
pb.directory(new File("/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/localhostNode01Cell/Svc_war.ear/Svc.war/"));
So final solution looks like:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("in doPost");
System.out.println("about to kick off ProcessBuilder");
ProcessBuilder pb = new ProcessBuilder("/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/localhostNode01Cell/Svc_war.ear/Svc.war/test.sh");
pb.redirectErrorStream(true);
pb.directory(new File("/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/localhostNode01Cell/Svc_war.ear/Svc.war/"));
Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
int ch;
while ((ch = br.read()) != -1)
System.out.println((char)ch);
br.close();
try {
int exitVal = process.waitFor();
System.out.println("Exit Value: " + exitVal);
} catch (InterruptedException e) {
e.printStackTrace();
}
Upvotes: 2
Reputation: 500663
The script runs fine from command line, using same command as I'm passing with .start().
I bet the issue is that when you test it from the command line, you are doing it under one user account, and WebSphere runs it under another account. The two accounts could have different access rights, different $PATH
settings etc. The fact that the shell script works under one account doesn't mean it would work under the other. You need to test that.
Upvotes: 0