title: Best method of triggering a shell script from Java
body: <p>I have a shell script which I'd like to trigger from a J2EE web app.</p>
  
  <p>The script does lots of things - processing, FTPing, etc - it's a legacy thing.</p>
  
  <p>It takes a long time to run.</p>
  
  <p>I'm wondering what is the best approach to this. I want a user to be able to click on a link, trigger the script, and display a message to the user saying that the script has started. I'd like the HTTP request/response cycle to be instantaneous, irrespective of the fact that my script takes a long time to run.</p>
  
  <p>I can think of three options:</p>
  
  <ul>
  <li>Spawn a new thread during the processing of the user's click. However, I don't think this is compliant with the J2EE spec. </li>
  <li>Send some output down the HTTP response stream and commit it before triggering the script. This gives the illusion that the HTTP request/response cycle has finished, but actually the thread processing the request is still sat there waiting for the shell script to finish. So I've basically hijacked the containers HTTP processing thread for my own purpose.</li>
  <li>Create a wrapper script which starts my main script in the background. This would let the request/response cycle to finish normally in the container.</li>
  </ul>
  
  <p>All the above would be using a servlet and Runtime.getRuntime().exec().</p>
  
  <p>This is running on Solaris using Oracle's OC4J app server, on Java 1.4.2.</p>
  
  <p>Please does anyone have any opinions on which is the least hacky solution and why?</p>
  
  <p>Or does anyone have a better approach?  We've got Quartz available, but we don't want to have to reimplement the shell script as a Java process.</p>
  
  <p>Thanks.</p>