I found two solutions for the problem:
1. Run the java code to print the result of the processBuilder.environment(). This shows the properties, paths etc where AEM is using to execute. the output looks like this
{PATH=/usr/java/latest/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/crx/.local/bin:/home/crx/bin, HISTCONTROL=ignoredups, LESSOPEN=||/usr/bin/lesspipe.sh %s, SHELL=/bin/bash, HISTSIZE=1000, OLDPWD=/home/crx, XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt, USER=crx, LANG=en_US.UTF-8, XDG_SESSION_ID=c1, MAIL=/var/spool/mail/crx, HOSTNAME=southwest-stage-20161004A-author1uswest1, TMOUT=600, NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat, LOGNAME=crx, XDG_RUNTIME_DIR=/run/user/1005, PWD=/mnt/crx/author, LS_COLORS=, HISTTIMEFORMAT=%F %T , HOME=/home/crx, SHLVL=1, _=/bin/setsid}If you observe closely PWD=/mnt/crx/author is the path where AEM internal process is looking at, USR=crx is the User that AEM is using internally to run the code.
So i copied my "shell script" to "/mnt/crx/author" and also gave execute permission to user "crx".
2. Other alternative is to add your shell script to the global PATH so it is available. For example you can put it under "/usr/local/bin".
Servlet Code snippet:
public class AssetsServlet extends SlingAllMethodsServlet { protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException { log.info("\n.....AssetsServlet doGet"); try { ScriptHelper.executeScript(""); .... .... ScriptHelper Code Snippet
public class ScriptHelper { public static void executeScript(String scriptPath) { log.info("...ScriptHelper.executeScript()"); try { /** Commands for server * "vltexport.sh" - Name of shellscript * "/apps/swa" - Source folder. I'm using these to be copied over' * "/mnt/crx/author/vltdata" - Destination folder */ String[] sendCommand = {"vltexport.sh", "/apps/swa", "/mnt/crx/author/vltdata"}; ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.directory(new File("/mnt/crx/author/vltdata")); log.info("Command options="+Arrays.asList(sendCommand)); processBuilder.command(sendCommand); log.info("Command="+processBuilder.command()); processBuilder.redirectErrorStream(true); //new log.info("Environment:::\n"+processBuilder.environment()); log.info("... starting to execute vltexport command"); final Process process = processBuilder.inheritIO().start(); // old final int errCode = process.waitFor(); log.info("... Complete with Code(0:Success,127:Fail) = "+errCode); log.info("Echo Output:\n" + output(process.getInputStream())); } catch(InterruptedException ie) { log.info("Thrown InterruptedException. Msg="+ie.getMessage()); } catch (Exception e) { log.info( e.getClass().getName() + " -- Error:"+e.getMessage()); //e.printStackTrace(); } }Sample script - vltexport.sh
#!/bin/bash export PATH=$PATH:/mnt/crx/author/vault-cli-3.1.16/bin vlt --credentials admin:admin export -v http://localhost:4502/crx $1 $2