Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
./run.sh - Tomcat Multi-Instance Run Shell Script (run.sh) Based on Springsource
#!/bin/bash
# ==================================================================
# ______ __ _____
# /_ __/___ ____ ___ _________ _/ /_ /__ /
# / / / __ \/ __ `__ \/ ___/ __ `/ __/ / /
# / / / /_/ / / / / / / /__/ /_/ / /_ / /
#/_/ \____/_/ /_/ /_/\___/\__,_/\__/ /_/
# Multi-instance Apache Tomcat installation with a focus
# on best-practices as defined by Apache, SpringSource, and MuleSoft
# and enterprise use with large-scale deployments.
# Credits:
# Google -> Couldn't survive without it
# Stackoverflow.com -> Community support
# SpringSource -> Specifically best-practices and seminars (Expert Series)
# Based On:
# http://www.springsource.com/files/uploads/tomcat/tomcatx-performance-tuning.pdf
# http://www.springsource.com/files/u1/PerformanceTuningApacheTomcat-Part2.pdf
# http://www.springsource.com/files/uploads/tomcat/tomcatx-large-scale-deployments.pdf
# Created By: Terrance A. Snyder
# URL: http://www.terranceasnyder.com, http://shutupandcode.net
# Best Practice Documentation:
# http://terranceasnyder.com/2011/05/tomcat-best-practices/
# Looking for the latest version?
# github @ https://github.com/terrancesnyder
# ==================================================================
# default tomcat version
TOMCAT_VERSION="apache-tomcat-7.0.14"
# ensure we always grab the current shell scripts
source ~/.bashrc
# user arguments
HTTP_PORT="$1"
ACTION="$2"
IP="$( ifconfig eth0 | awk '/inet addr/ {split ($2,A,":"); print A[2]}' )"
# directory
SCRIPT=$(readlink -f $0)
DIRECTORY=`dirname $SCRIPT`
if [ -z "$1" -o -z "$2" ]; then
echo "usage: run.sh <configuration> [start|stop]"
exit -1
fi
if [ `whoami` != "tomcat" ]; then
echo "error: you are not running under the tomcat user"
exit -1
fi
SHUTDOWN_PORT=$(($HTTP_PORT+1))
JMX_PORT=$(($HTTP_PORT+2))
JPDA_PORT=$(($HTTP_PORT+3))
export JRE_HOME="/opt/dev/java"
export JAVA_HOME="/opt/dev/java"
export JPDA_ADDRESS="$JPDA_PORT"
export JPDA_TRANSPORT="dt_socket"
export CATALINA_BASE="$DIRECTORY/$HTTP_PORT"
export CATALINA_HOME="$DIRECTORY/$TOMCAT_VERSION"
export CATALINA_CONF="$DIRECTORY/shared/server.xml"
export CATALINA_PID="$CATALINA_BASE/logs/catalina.pid"
export LOGGING_CONFIG="-Djava.util.logging.config.file=$DIRECTORY/shared/logging.properties"
# check if tomcat installed if not download it
if [ ! -d "$DIRECTORY/$TOMCAT_VERSION" ]; then
echo "Downloading Apache Tomcat 7.0.14 from Apache..."
wget http://www.carfab.com/apachesoftware/tomcat/tomcat-7/v7.0.14/bin/apache-tomcat-7.0.14.zip > /dev/null
echo "Extracting Tomcat..."
unzip apache-tomcat-7.0.14.zip
echo "Removing downloaded zip..."
rm -rf apache-tomcat-7.0.14.zip
echo "Changing scripts to executable..."
chmod +x ./apache-tomcat-7.0.14/bin/*.sh
fi
if [ ! -d "$CATALINA_BASE" ]; then
echo "error: the configured folder does not exist '$CATALINA_BASE'"
exit -1
fi
# default jmx options - secured by default
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote"
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.access.file=$DIRECTORY/shared/jmxremote.access"
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.password.file=$DIRECTORY/shared/jmxremote.password"
export CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=$IP"
# java opts are primary and we need these to define the http ports
export JAVA_OPTS="$JAVA_OPTS -Dhttp.port=$HTTP_PORT"
export JAVA_OPTS="$JAVA_OPTS -Dshutdown.port=$SHUTDOWN_PORT"
echo ""
echo " ______ __ _____"
echo " /_ __/___ ____ ___ _________ _/ /_ /__ /"
echo " / / / __ \/ __ __ \/ ___/ __ / __/ / / "
echo " / / / /_/ / / / / / / /__/ /_/ / /_ / / "
echo "/_/ \____/_/ /_/ /_/\___/\__,_/\__/ /_/ "
echo " "
echo " "
echo "IP: $IP | HTTP: $HTTP_PORT | JPDA Port: $JPDA_PORT | JMX Port: $JMX_PORT"
echo "_______________________________________________"
echo ""
exec "$CATALINA_HOME/bin/catalina.sh" jpda "$ACTION" -config $CATALINA_CONF
Tomcat
/S
Owner

terrancesnyder commented Jun 4, 2011

Nice console output showing all the Java options. Also updated to include fancy ascii-art as well as IP address resolution for remote JMX.

tomcat@selenium:/opt/dev/tomcat$ ./run.sh 7051 start

  ______                           __     _____
 /_  __/___  ____ ___  _________ _/ /_   /__  /
  / / / __ \/ __  __ \/ ___/ __  / __/     / /
 / / / /_/ / / / / / / /__/ /_/ / /_      / /
/_/  \____/_/ /_/ /_/\___/\__,_/\__/     /_/


IP: 192.168.1.17 | HTTP: 7051 | JPDA Port: 7054 | JMX Port: 7053
_______________________________________________

Using CATALINA_OPTS:
>>  -Dcom.sun.management.jmxremote
>>  -Dcom.sun.management.jmxremote.port=7053
>>  -Dcom.sun.management.jmxremote.ssl=false
>>  -Dcom.sun.management.jmxremote.authenticate=false
>>  -Dcom.sun.management.jmxremote.access.file=/opt/dev/tomcat/shared/jmxremote.access
>>  -Dcom.sun.management.jmxremote.password.file=/opt/dev/tomcat/shared/jmxremote.password
>>  -Djava.rmi.server.hostname=192.168.1.17
>>  -Xms512m
>>  -Xmx512m
>>  -XX:MaxPermSize=256m
>>  -Xss192k
>>  -XX:+UseParallelGC
>>  -XX:MaxGCPauseMillis=1500
>>  -XX:GCTimeRatio=9
>>  -server
>>  -XX:+DisableExplicitGC

Using JAVA_OPTS:
>>  -Dhttp.port=7051
>>  -Dshutdown.port=7052
_______________________________________________

Using CATALINA_BASE:   /opt/dev/tomcat/7051
Using CATALINA_HOME:   /opt/dev/tomcat/apache-tomcat-7.0.14
Using CATALINA_TMPDIR: /opt/dev/tomcat/7051/temp
Using JRE_HOME:        /opt/dev/java
Using CLASSPATH:       /opt/dev/tomcat/apache-tomcat-7.0.14/bin/bootstrap.jar:/opt/dev/tomcat/apache-tomcat-7.0.14/bin/tomcat-juli.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment