title: Running shell script on tomcat7
body: <p>I have been breaking my head for two days trying to fix the file permissions for my tomcat7 server. I have a library class (.jar file included in myapp/WEB-INF) which needs to run a shell script. The library is written by me and works fine within NetBeans ie. no hassle in creating,reading and deleting files. That is because NetBeans runs the program as blumonkey(my username on my Ubuntu System). But when I import this into tomcat and run it, tomcat "executes" the command, produces no definite output, tries to check for a file(which will be generated when the script succeeds) and throws a FileNotFoundException.</p>
  
  <p>More Details as follows:</p>
  
  <ul>
  <li>Tomcat7 installed using apt-get, has its data in 2 locations - /var/lib/tomcat7 with conf and webapps folders and /usr/share/tomcat7 with the bin and lib folders</li>
  <li>The user uploads a .zip file which is stores to /home/blumonkey/data. Rest of the program runs on the documents stored here. All new folders/files uploaded by tomcat have, obviously, tomcat7 as the owner.</li>
  <li>I have tried things like changing the ownership to blumonkey, adding tomcat7 to blumonkey user group but none of the methods worked (Somewhere around here I probably messed up changing permissions carelessly :/ ). Apparently tomcat7 is unable to process on the files it owns.(How can this be?). </li>
  <li>The script works when I run it in the terminal. But it doesn't work when I do a <code>sudo -u tomcat7 script.sh</code>, ie run it as tomcat7. It just exits with no message. I doubt that this it what is happening as I have tried to debug by redirecting the errors and outputs in <code>ProcessBuilder</code> but they came empty.</li>
  </ul>
  
  <p>Any help regarding how to fix the issue and get the script running would be greatly appreciated. Please comment if you need any more info.</p>
  
  <p>The code for script execution</p>
  
  <pre><code>     private static void RunShellCommandFromJava(String command,String fn, String arg1,String arg2) throws Exception
     {
    try
      {
           System.out.println(System.getProperty("user.name")); 
           ProcessBuilder pbuilder = new ProcessBuilder("/bin/bash",command,fn,arg1,arg2);
           System.out.println(pbuilder.command());
              pbuilder.redirectErrorStream(true);
              Process p = pbuilder.start();
          p.waitFor();
      }
      catch(Exception ie)
      {
          throw ie;
      }
  }
  </code></pre>
  
  <p>The command which needs to be executed </p>
  
  <pre><code>"/bin/bash /abs/path/to/script.sh  /abs/path/to/doc/in/data-folder maxpages=30 maxsearches=3"
  </code></pre>
  
  <p><strong>PS</strong> : I have followed <a href="https://stackoverflow.com/questions/12533799/execute-shell-command-in-tomcat">this</a> question but it didn't help. I also tried other options like <code>Runtime.exec()</code>, <code>bash</code>,<code>/bin/bash/</code> and <code>/bin/bash/ -c</code>, some of them don't work at all, others give no results.</p>