Friday 20 June 2014

How to make startup script for automated tomcat start after reboot on Linux

I you are good system admin then one of first things after installation of some new application is to make that application start after system reboot!
Some application(glassfish, websphere) have mechanism to make startup script during installation but there are application that do not do that!

In case that your developers team create new JAVA application that is running under tomcat, here is how my(and yours) startup script (should)look like

#!/bin/bash
# Run-level Startup script for the Tomcat
#
# chkconfig: 345 99 19
# description: Startup/Shutdown Tomcat
#
# Startup script for the Tomcat
CATALINA_HOME=location_of_tomcat_folder
JAVA_HOME=location_of_java_folder
PATH=$PATH:$CATALINA_HOME/bin:$JAVA_HOME/bin
DISPLAY=unix:1.0
export CATALINA_HOME JAVA_HOME PATH DISPLAY
cd $CATALINA_HOME/bin
start() {
        echo -n $"Starting Tomcat: "
        su user_that_is_running_application -c "startup.sh"
        [ RETVAL=$? ] && touch /var/lock/subsys/tomcat
        echo
        return $RETVAL
}
stop() {
        echo -n $"Stopping Tomcat: "
        su
user_that_is_running_application -c "shutdown.sh"
        [ RETVAL=$? ] && touch /var/lock/subsys/tomcat
        echo
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  *)
        echo $"Usage: $prog {start|stop}"
        exit 1
esac

exit $RETVAL


Location of this script should be in /etc/init.d/ folder. You can named it like you want. We can called it tomcat-aps.

Ok, so how to test it if your script is running OK?

First,make it executable!

#chmod +x tomcat-aps

Now,  check if there are any java process running or even better go to log folder of your tomcat application. This should be located in /location_of_tomcat/logs and see content of catalina.out file. It is best to tail this file

#tail -f /location_of_tomcat/logs/catalina.out

 and see changes in your tomcat application.

OK, now lets start application! Open new terminal window and ...

#/etc/init.d/tomcat-aps start

In your catalina.out file if everything is OK, you shoul see something like this


Jun 20, 2014 12:08:32 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
Jun 20, 2014 12:08:32 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8083
Jun 20, 2014 12:08:32 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 20, 2014 12:08:32 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/16  config=null
Jun 20, 2014 12:08:32 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 9063 ms

Your application is up and running. In case that you see errors, well you have problem with starting of your application with this script! We will not troubleshot that in this post.

If every thing is OK, check in what runlevel your server is running,

#runlevel
N 3

We are doing this to see in what rcX.d folder wee will put link to are startup script!

#cd /etc/rc3.d/
#ln -s /etc/init.d/tomcat-aps S99tomcat-aps

Now, when you server in booting in runlevel 3, your tomcat-aps script will execute and your application will be up and running!

No comments: