Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing a bash script from JSP pages

Status
Not open for further replies.

jaillet

Programmer
Nov 22, 2002
3
CH
Hello,
I try to automate a Database export from a JSP page.
I made a bash script and when I call it from a Telnet it's work fine but when a try from my jsp page that dosent work.
Note it's work with a simple command like "ls" but not with my script. I have checked the right for the script file and if a do manualy thru Telnet it's work !!


//----my JSP page ---
Process exportDb = Runtime.getRuntime().exec("myBatchScript");
try { exportDb.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(exportDb.getInputStream()));
while(br.ready()) {
exportOutput = br.readLine();
out.println(exportOutput+&quot;<br>&quot;);
}
br.close();
} catch (InterruptedException ie){
ie.printStackTrace();
System.out.println(&quot;A problem occured whith process&quot;);
}

TKS
Olivier
 
Hi jaillet:

I have a script called from a jsp that works fine.

My script calling has two differences, first I call it from a java bean included in a jsp, and second I put the whole path to run the script, starting in /. You said that it works with an ls, so I don't think the path could be the problem. Probably, the Runtime that you get from a JSP is the runtime of the server JVM, and depending on the webserver it couldn't have enough permissions to execute the batch file. It works fine in IPlanet WevServer 4.1.

Here is the function that calls the script:

Code:
public void correrSearch(String par){ 

    Runtime rt = Runtime.getRuntime();
     try {   
  	   
	   String path = &quot;/export/home/sasmdev/busquedaLdap.sh&quot;;
  
	   String[] args = {path,par};

           Process p = rt.exec(args);  

	   BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));	      

       try{                        
           while(true){ 
				String leido = br.readLine();            
				if (leido == null){ 
					break; 
				}							}		 	    		        
			}         
			br.close(); 
       } 
       catch (IOException e){       
		   System.out.println(&quot;Error en lectura &quot;+e);
       }        
     } 
     catch (Exception e){
	     System.out.println(&quot;Error &quot;+e);
	 }
   }


You can get the errorstream to see what happens:
Code:
	   BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));

Hope it helps
Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top