bash unix processbuilder from java not running bash unix processbuilder from java not running unix unix

bash unix processbuilder from java not running


First, are you sure bash is definitely at /usr/bin? Second, you probably need to tell the ProcessBuilder what directory it should use as the cwd when running the process, otherwise it will try and create myfile.txt in whatever is the current directory of the servlet container, typically somewhere you don't have write access. And thirdly, when you run a process from java the output of the process is passed back to java via input streams on the process object, it doesn't go straight to stdout, so you need to read the streams to see the result

ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash", "-c", "echo HELLO > myfile.txt");pb.directory(...);pb.redirectErrorStream(true);Process p = pb.start();IOUtils.copy(p.getInputStream(), System.out);p.waitFor();


String echo = "echo 'hello' > myfile.txt";ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash", "-c", echo);pb.start();


Check your error handling; you're probably swallowing an exception somewhere because there is no bash in /usr/bin, so you're getting a "file not found" exception (or similar).

Try "/bin/bash" instead. The rest should work.

Also note that relative paths won't work after you deploy you app since it will be relative to the process running the Java VM which isn't what you expect, want or could use. Ask your ServletContext for a path with getRealPath()