1 Reply Latest reply on Jan 11, 2008 3:27 PM by Ragav Gomatam

    how to call unix shell script from web app?

    Dig Digger Newbie

      Hi,

      I would like to call unix shell scripts from my web app and get the exit code from the job passed back to the web user. The job is short enough for the call to be done synchronously.
      What would be a robust way of implementing this functionality? My JBoss version is 4.0.5.

      -Steve

        • 1. Re: how to call unix shell script from web app?
          Ragav Gomatam Novice

          Code is as below :-



          import java.io.*;
          
          /** Demonstrate how to run an external program. **/
          public class MyServlet implements HttpServlet{
          
           /** From the main run an servlet . **/
           public doGet( request,response) {
           try {
           Runtime rt = Runtime.getRuntime (); // step 1
          
           Process process = rt.exec ("/call/some/shell"); // step 2
          
           InputStreamReader reader = // step 3
           new InputStreamReader ( process.getInputStream () );
          
           BufferedReader buf_reader =
           new BufferedReader ( reader ); // step 4.
          
           String line;
           while ((line = buf_reader.readLine ()) != null)
           System.out.println (line);
          
           }
           catch (IOException e) {
           System.out.println (e);
           }
           }
          } // servlet end


          Make sure you grab the Process & flush the err stream, input tsream & out put stream...Other wise due to insufficient length of err buffers in OS this would hang