Tomcat startup script

home of a content challenger

Tomcat startup script

You know this situation where you install tomcat in “some location” with “some user” being able to stop and start it? Not going the official (linux) way and installing it through your package manager?

So I have made a small script, on Solaris, to have stop and start functionality in those locations for that special user (in my case dmtomcat):

# tomcat stop und start Skript
#

# Some things that run always
set -e
NAME=tomcat
TOMCAT_TIMEOUT=60
TOMCAT_USER=dmtomcat

# Directory where the Tomcat 6 binary distribution resides
CATALINA_HOME=/opt/tomcat/current

# Define other required variables
export CATALINA_PID="$CATALINA_HOME/temp/$NAME.pid"
CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"

# Escape any double quotes in the value of JAVA_OPTS
JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')"

if [ `/usr/xpg4/bin/id -u` -ne `/usr/xpg4/bin/id -u $TOMCAT_USER` ]; then
        echo "You need to be" $TOMCAT_USER "to run this script"
        exit 1
fi

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    # Test to see whether tomcat is already running
    if [ -f "$CATALINA_PID" ]; then
        # We have a pid file, not good, do some sanity
        PID=`exec cat $CATALINA_PID`
        PID_USER=`ps -ef | grep $PID | grep tomcatjava | awk '{ print $1}'`

        # Is the pid assigned to the tomcat user and using tomcatjava?
        if [ "X"$PID_USER == "X"$TOMCAT_USER ]; then
            echo "It seems that tomcat is already running with pid" $PID
            exit 1
        fi

        # PID file was there but the above sanity check failed
        # So we can remove the PID file
        rm $CATALINA_PID
    fi

    if [ "$2" == "clean" ]; then
        echo "** Cleaning tomcat work directory"
        rm -rf $CATALINA_HOME/work/catalina/localhost
    fi

    echo "** Starting tomcat service..."
    $CATALINA_SH start
    echo "All done. Please enter 'tailout' to monitor the startup process"
    ;;
  stop)
    echo "** Stopping tomcat service..."
    echo "** Will take" $TOMCAT_TIMEOUT "seconds"
    $CATALINA_SH stop $TOMCAT_TIMEOUT -force
    ;;
  restart)
    echo "** Stopping tomcat service..."
    echo "** Will take" $TOMCAT_TIMEOUT "seconds"
    $CATALINA_SH stop $TOMCAT_TIMEOUT -force

    if [ "$2" == "clean" ]; then
        echo "** Cleaning tomcat work directory"
        rm -rf $CATALINA_HOME/work/catalina/localhost
    fi

    echo "** Starting tomcat service..."
    $CATALINA_SH start
    echo "All done. Please enter 'tailout' to monitor the startup process"
    ;;

  *)
    echo "Usage: tomcat-service {start|stop|restart} [clean]"
    echo
    echo "       If the clean option is provided the tomcat work"
    echo "       directory will be cleaned before starting the service"
    exit 1
    ;;
esac

exit 0

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: