I would like to know how to run a shell script on Tomcat startup, that is when catalina.log prints \"INFO: Server startup in xxx ms\"
Thanks in advance, bye
At Tomcat 7, you can accomplish that through a custom listener in the server.xml file at tomcat's configuration:
CustomEventHootListener.java:
package your.domain;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
public class CustomEventHookListener implements LifecycleListener {
@Override
public void lifecycleEvent(LifecycleEvent arg0) {
Lifecycle lifecycle = arg0.getLifecycle();
if (lifecycle == null) {
return;
}
String type = arg0.getType();
if (type == null) {
return;
}
String stateName = lifecycle.getStateName();
if (stateName == null) {
return;
}
if (type.equals("after_start") && stateName.equals("STARTED")) {
startPostInitScript();
}
}
private void startPostInitScript() {
// Non-blocking please.
Thread t = new Thread() {
@Override
public void run() {
try {
super.run();
String script = "/path/to/your-script.sh";
// inheritIO to output things to JVM.
new ProcessBuilder().command(script).inheritIO().start().waitFor();
} catch (Throwable e) {
e.printStackTrace();
}
}
};
t.setDaemon(true);
t.start();
}
}
You can see a functional example at https://github.com/Valemobi/tomcat-events-hook