#!/bin/bash # # Linux init script for the Apache Tomcat servlet container. # # chkconfig: 2345 96 14 # description: The Apache Tomcat servlet container. # processname: tomcat # config: /opt/tomcat/conf/tomcat-env.sh # # Copyright (c) 2007, 2008 Jason Brittain # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ---------------------------------------------------------------------- # $Id: init.linux 125 2009-06-08 07:46:53Z jasonb $ # # Author: Jason Brittain # Source function library. if [ -x /etc/rc.d/init.d/functions ]; then . /etc/rc.d/init.d/functions fi APP_ENV="/opt/tomcat/conf/tomcat-env.sh" # Source the app config file, if it exists. [ -r "$APP_ENV" ] && . "${APP_ENV}" # The path to the Tomcat start/stop script. TOMCAT_SCRIPT=$CATALINA_HOME/bin/catalina.sh # The name of this program. PROG="$0" # Resolve links - $0 may be a soft link. while [ -h "$PROG" ]; do ls=`ls -ld "$PROG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '.*/.*' > /dev/null; then PROG="$link" else PROG=`dirname "$PROG"`/"$link" fi done PROG="`basename $PROG`" # If TOMCAT_USER is not set, use "tomcat". if [ -z "$TOMCAT_USER" ]; then TOMCAT_USER="tomcat" fi # Since the daemon function will run $TOMCAT_SCRIPT, no environment # stuff should be defined here anymore. Please use the # /opt/tomcat/conf/tomcat-env.sh file instead. let RETVAL=0 JVM_PID="0" JVM_RUNNING="false" start() { echo -n "Starting $PROG: " checkJvmRunning if [ "$JVM_RUNNING" == "true" ]; then echo -n "\"$JVM_ID\" JVM process already running. " else # Raise the process's maximum number of file descriptors to 8192. ulimit -n 8192 # Exit with an explanation if our JAVA_HOME isn't found. if [ ! -d "$JAVA_HOME" ]; then echo "JAVA_HOME of $JAVA_HOME not found." echo "See ${APP_ENV}" if [ -x /etc/rc.d/init.d/functions ]; then echo -n "Starting $PROG: " echo_failure echo fi return 1 fi # Start Tomcat, running as the $TOMCAT_USER. if [ "$USER" == "$TOMCAT_USER" ]; then # We're already the $TOMCAT_USER so just exec the script. exec bash -c "set -a; . $APP_ENV; $TOMCAT_SCRIPT start" \ >/dev/null 2>&1 else if [ -x /etc/rc.d/init.d/functions -a -x /sbin/runuser ]; then runuser -s /bin/bash - $TOMCAT_USER \ -c "set -a; . $APP_ENV; $TOMCAT_SCRIPT start" &>/dev/null else su - $TOMCAT_USER -c "/bin/bash -c \ \"set -a; . $APP_ENV; $TOMCAT_SCRIPT start\"" \ >/dev/null 2>&1 fi fi let RETVAL=$? # If the return value is zero, then the attempt to start it is # good so far. if [ $RETVAL -eq 0 ]; then # Sleep some seconds while Tomcat begins to start, then check it. sleep 7 checkJvmRunning if [ "$JVM_RUNNING" == "false" ]; then let RETVAL=1 fi fi fi # Output "[ OK ]" or "[ FAILED ]" if [ $RETVAL -eq 0 ]; then if [ -x /etc/rc.d/init.d/functions ]; then echo_success echo else echo "[ OK ]" fi else if [ -x /etc/rc.d/init.d/functions ]; then echo_failure echo else echo "[ FAILED ]" fi fi return $RETVAL } stop() { echo -n "Stopping $PROG: " checkJvmRunning if [ "$JVM_RUNNING" == "true" ]; then # Exit with an explanation if our JAVA_HOME isn't found. if [ ! -d "$JAVA_HOME" ]; then echo "JAVA_HOME of $JAVA_HOME not found." echo "See ${APP_ENV}" echo -n "Stopping $PROG: " if [ -x /etc/rc.d/init.d/functions ]; then echo_failure echo else echo "[ FAILED ]" fi return 1 fi # Stop Tomcat, running as the $TOMCAT_USER. We also unset any # JVM memory switches -- the stop client shouldn't start with those. if [ "$USER" == "$TOMCAT_USER" ]; then # We're already the $TOMCAT_USER so just exec the script. exec bash -c "set -a; . $APP_ENV; shopt -s extglob; \ export JAVA_OPTS=\"\${JAVA_OPTS//-Xm[sx]+([0-9])[mM]}\"; \ shopt -u extglob; $TOMCAT_SCRIPT stop" &>/dev/null else if [ -x /etc/rc.d/init.d/functions -a -x /sbin/runuser ]; then runuser -s /bin/bash - $TOMCAT_USER \ -c "set -a; . $APP_ENV; shopt -s extglob; \ export JAVA_OPTS=\"\${JAVA_OPTS//-Xm[sx]+([0-9])[mM]}\"; \ shopt -u extglob; $TOMCAT_SCRIPT stop" &>/dev/null else su - $TOMCAT_USER -c "/bin/bash -c \ \"set -a; . $APP_ENV; shopt -s extglob; \ export JAVA_OPTS=\"\${JAVA_OPTS//-Xm[sx]+([0-9])[mM]}\"; \ shopt -u extglob; $TOMCAT_SCRIPT stop\"" &>/dev/null fi fi let RETVAL=$? if [ $RETVAL -eq 0 ]; then checkJvmRunning if [ "$JVM_RUNNING" == "true" ]; then # Loop here until either Tomcat shuts down on its own, or # until we've waited longer than SHUTDOWN_WAIT seconds. let count=0 until [ "`ps --pid $JVM_PID | grep -c $JVM_PID`" == "0" ] || [ $count -gt $SHUTDOWN_WAIT ] do if [ $count -eq 0 ]; then echo fi echo "Waiting for processes to exit.." sleep 1 let count=$count+1 done if [ $count -gt $SHUTDOWN_WAIT ]; then # Tomcat is still running, so we'll send the JVM a # SIGTERM signal and wait again. echo "Sending the Tomcat processes a SIGTERM asking them" \ "to shut down gracefully.." /bin/kill -s SIGTERM $JVM_PID &>/dev/null # Loop here until either Tomcat shuts down on its own, or # until we've waited longer than SHUTDOWN_WAIT seconds. let count=0 until [ "`ps --pid $JVM_PID | grep -c $JVM_PID`" \ == "0" ] || [ $count -gt $SHUTDOWN_WAIT ] do echo "Waiting for processes to exit.." sleep 1 let count=$count+1 done if [ $count -gt $SHUTDOWN_WAIT ]; then # Tomcat is still running, and just won't shut down. # We'll kill the JVM process by sending it a SIGKILL # signal and wait again for the JVM process to die. echo "Killing processes which didn't stop after" \ "$SHUTDOWN_WAIT seconds." /bin/kill -s SIGKILL $JVM_PID &>/dev/null # Loop here until either Tomcat shuts down on its own, # or until we've waited longer than SHUTDOWN_WAIT # seconds. let count=0 until [ "`ps --pid $JVM_PID | grep -c $JVM_PID`" \ == "0" ] || [ $count -gt $SHUTDOWN_WAIT ] do echo "Waiting for processes to exit.." sleep 1 let count=$count+1 done if [ $count -gt $SHUTDOWN_WAIT ]; then # The JVM process won't shut down even with a # SIGKILL, so there is something really wrong. echo "The \"$JVM_ID\" JVM process is wedged and" \ "won't shut down even when it is" echo "sent a SIGKILL." echo "Process ID $JVM_PID." # Right here we may want to email an administrator. let RETVAL=1 fi fi # We need to sleep here to make sure the JVM process dies. sleep 2 fi fi fi fi # Output "[ OK ]" or "[ FAILED ]" if [ $RETVAL -eq 0 ]; then if [ -x /etc/rc.d/init.d/functions ]; then echo_success echo else echo "[ OK ]" fi else if [ -x /etc/rc.d/init.d/functions ]; then echo_failure echo else echo "[ FAILED ]" fi fi return $RETVAL } getJvmPid() { JVM_PID="`ps awwx | grep \"jvm=$JVM_ID \" | grep -v grep | head -n 1 | \ cut -c -5`" } checkJvmRunning() { getJvmPid if [ "$JVM_PID" != "" ]; then JVM_RUNNING="true" else JVM_RUNNING="false" fi } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop if [ $RETVAL -eq 0 ]; then start fi ;; status) SERVICE_NAME="$PROG" checkJvmRunning if [ "$JVM_RUNNING" == "true" ]; then echo "$SERVICE_NAME (pid $JVM_PID) is running." let RETVAL=0 else echo "$SERVICE_NAME is not running." let RETVAL=1 fi exit $RETVAL ;; condrestart) # If it's already running, restart it, otherwise don't start it. checkJvmRunning if [ "$JVM_RUNNING" == "true" ]; then stop if [ $RETVAL -eq 0 ]; then start fi fi ;; *) echo "Usage: $PROG {start|stop|restart|status|condrestart}" let RETVAL=1 exit $RETVAL esac exit $RETVAL