Chapter 3. Starting a Tomcat Instance

Table of Contents

Init script

There are lots of quirks which are encountered when trying to start up Tomcat. See the Tomcat Quirks chapter about problems with starting up Tomcat.

If you are restarting Tomcat, make sure that it really does shut down, because the Tomcat-supplied scripts do not tell you if your shutdown attempt fails. Often, a startup problem is really a shutdown problem... if you could just stop the damned thing, it would start just fine. See the Tomcat Quirks about fixing that.

Init script

If your OS supplies a good init script, you should probably use that, since it will probably use configuration files and users which have been set up by the OS-specific installation scripts. Suse-users should be aware that most of the runtime configurations for the startup script are made in /etc/sysconfig/j2ee. Most significantly, the setting CATALINA_BASE.

Example 3.1. Multi-Tomcat-instance generic UNIX init script

#!/bin/sh -p

# $Id: tomcat,v 1.8 2005/11/01 15:32:16 blaine Exp $

export CATALINA_HOME CATALINA_BASE JAVA_HOME CATALINA_PID JAVA_OPTS
BASE_PARENT_PATH=/home/tomcat
DFLT_CATALINA_HOME=/usr/local/tomcat-5.0.28
DFLT_JAVA_HOME=/usr/local/java
DFLT_TOMCAT_OWNER=tomcat
# Note:  Set JAVA_OPT to increate RAM usage, etc.
# The "headless" setting is needed for any webapps making use of
#   an awt graphics context, like with JFreeChart or JasperReport.
DFLT_JAVA_OPTS="-Djava.awt.headless=true -ms512M -mx512M"

case "$1" in
    start|stop)
        case "$1" in start) SCRIPT=startup.sh;; stop) SCRIPT=shutdown.sh;; esac
        for catworkdir in $BASE_PARENT_PATH/*/work/Catalina; do
            workdir=`dirname $catworkdir`
            CATALINA_BASE=`dirname $workdir`
            [ -d "$CATALINA_BASE" ] || {
        echo "Aborting.  CATALINA_BASE '$CATALINA_BASE' not a directory?" 1>&2
                exit 1
            }

            # Reset all vars to defaults
            CATALINA_HOME="$DFLT_CATALINA_HOME"
            JAVA_HOME="$DFLT_JAVA_HOME"
            TOMCAT_OWNER="$DFLT_TOMCAT_OWNER"
            JAVA_OPTS="$DFLT_JAVA_OPTS"

            BASE_NAME=`basename $CATALINA_BASE`
            case "$BASE_NAME" in test*) continue;; template*) continue;; esac
            CATALINA_PID=$CATALINA_BASE/temp/tomcat.pid

            # Allow instance-specific environment file to override any settings
            [ -f "$CATALINA_BASE" ] && [ -r "$CATALINA_BASE/instance.env" ] &&
            . "$CATALINA_BASE/instance.env"

            echo "Starting Tomcat instance '$BASE_NAME'..." 1>&2
            if su "$TOMCAT_OWNER" -c $CATALINA_HOME/bin/$SCRIPT; then
                echo Success
            else
                echo Failed
            fi 1>&2
        done
        ;;
    purge)
        /etc/init.d/tomcat stop || exit 1
        for catworkdir in $BASE_PARENT_PATH/*/work/Catalina; do
            workdir=`dirname $catworkdir`
            CATALINA_BASE=`dirname $workdir`
            [ -d "$CATALINA_BASE" ] || {
        echo "Aborting.  CATALINA_BASE '$CATALINA_BASE' not a directory?" 1>&2
                exit 1
            }
            BASE_NAME=`basename $CATALINA_BASE`
            case "$BASE_NAME" in test*) continue;; template*) continue;; esac
            echo "Purging Tomcat instance '$BASE_NAME'..." 1>&2
            rm -r $CATALINA_BASE/logs/*
        done
        exec /etc/init.d/tomcat start
        ;;
    restart)
        /etc/init.d/tomcat stop && exec /etc/init.d/tomcat start
        ;;
    *)
        echo "SYNTAX:  /etc/init.d/tomcat {start|stop|restart|purge}" 1>&2
        exit 2
        ;;
esac

exit 0

You can download it here. Explain settings for my Tomcat init script.