1) Create a new sh file example:
vi /etc/my_scripts/start_apache.sh
Content:
# execute your custom command to start apache depends of version / s.o. configs, etc
# /etc/init.d/httpd restart
# /etc/init.d/httpd start
# /etc/init.d/httpd stop
# /etc/init.d/httpd
# service httpd start
apachectl start
2) With your script finished, go to development of our Java class:
package mypackage;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class ServerControl {
public void executeScript(final String scriptName) {
BufferedWriter ouputCommand = null;
Runtime r = Runtime.getRuntime();
try {
StringBuffer startApacheCommand = new StringBuffer();
startApacheCommand.append("/etc/my_scripts/"+scriptName+"&");
Process child = r.exec("/bin/sh");
ouputCommand = new BufferedWriter(new OutputStreamWriter(child.getOutputStream()));
ouputCommand.write(startApacheCommand.toString() + "\n");
ouputCommand.flush();
} catch (IOException e) {
e.printStackTrace();
// catch the exception...
}
}
}
import java.io.IOException;
import java.io.OutputStreamWriter;
public class ServerControl {
public void executeScript(final String scriptName) {
BufferedWriter ouputCommand = null;
Runtime r = Runtime.getRuntime();
try {
StringBuffer startApacheCommand = new StringBuffer();
startApacheCommand.append("/etc/my_scripts/"+scriptName+"&");
Process child = r.exec("/bin/sh");
ouputCommand = new BufferedWriter(new OutputStreamWriter(child.getOutputStream()));
ouputCommand.write(startApacheCommand.toString() + "\n");
ouputCommand.flush();
} catch (IOException e) {
e.printStackTrace();
// catch the exception...
}
}
}
3) Now, build a jsp file to call developed java class:
vi /my_applications/example/my_script.jspx
...etc...
<jsp:useBean id="serverControl" class="mypackage.ServerControl" />
<jsp:scriptlet><![CDATA[
serverControl.executeScript("start_apache.sh");
// to call another stop_apache.sh script for example
// serverControl.executeScript("stop_apache.sh");
// to call another restart_apache.sh script for example
// serverControl.executeScript("restart_apache.sh");
// to call another restart_glassfish_domain.sh script for example
// serverControl.executeScript("restart_glassfish_domain.sh");
// to call another custom_script.sh script for example
// serverControl.executeScript("custom_script.sh");
]]></jsp:scriptlet>
...etc....
4) Now, only call jsp from your server:
http://localhost:/my_application/example/my_script.jspx
Nenhum comentário:
Postar um comentário