ProcessBuilder.start() returns 0 but doesn't execute shell script

怎甘沉沦 提交于 2019-12-06 06:09:59

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();
}

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")

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!