Main menu

Create init script for tomcat

If you are installing Tomcat from archive, probably you have to start and stop the service through tomcat bin directory. It’s works fine, but for day to day use it’s convenient to use init script for tomcat. This way you can start, stop, restart and schedule it at boot time. It’s also useful if yo want to mange multiple instances of tomcat and want to use different environment setting for each instances. You can export JAVA_HOME and JAVA_OPTS variable into init script and use different JVM or different JVM setting for each instances.

Here i have just created a simple service file which you can put on /etc/init.d directory and can use service command to manage it.

Create a file inside /etc/init.d directory and named it tomcat or whatever you want

#!/bin/bash                                              
##########################################                                     # 
# Source function library.
. /etc/init.d/functions
# Setting the limit
ulimit -n 65535
# Setting java environment veriables
export JAVA_HOME=/opt/jdk
export CATALINA_HOME=/home/tomcat
export JAVA_OPTS='-Xms512m -Xmx1024m -XX:PermSize=256M -XX:MaxPermSize=256M'
#Setting Tomcat startup and shutdown script path
export startup="$CATALINA_HOME/bin/startup.sh"
export shutdown="$CATALINA_HOME/bin/shutdown.sh"

start() {
        echo -n $"Starting Tomcat: "
        $startup && success || failure
        }
stop() {
        echo -n $"Stopping Tomcat: "
    $shutdown && success || failure
        rm -rf $CATALINA_HOME/work/Catalina/*
        rm -rf $CATALINA_HOME/temp/* && success || failure
        }
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 5
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

Now It’s done !! You can manage your tomcat instance through service command in any Linux distribution.

Note: This is an standard init script for tomcat. Please modify according your requirement.

FacebookTwitterGoogle+RSS