#!/bin/bash # # tomcat This shell script starts and stops Tomcat. # Based on CentOS startup script for Tomcat 5.0 but # has been modified for Tomcat 5.5 # # chkconfig: - 80 20 # ### BEGIN INIT INFO # Provides: tomcat # Required-Start: $network $syslog # Required-Stop: $network $syslog # Default-Start: # Default-Stop: # Description: tomcat start/stop (Tomcat 5.5 apache build with Sun Java 1.4 JDK) # process name: tomcat # pid file: /var/run/tomcat.pid # config: /etc/opt/tomcat/tomcat.conf ### END INIT INFO # Source the function library if [ -r "/etc/rc.d/init.d/functions" ]; then . /etc/rc.d/init.d/functions fi NAME="$(basename $0)" unset ISBOOT if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then NAME="${NAME:3}" ISBOOT="1" fi # For SELinux we need to use 'runuser' not 'su' if [ -x "/sbin/runuser" ]; then SU="/sbin/runuser" else SU="su" fi # Get the tomcat config (use this for environment specific settings) TOMCAT_CFG="/etc/opt/tomcat/tomcat.conf" if [ -r "$TOMCAT_CFG" ]; then . ${TOMCAT_CFG} fi # Get instance specific config file if [ -r "/etc/sysconfig/${NAME}" ]; then . /etc/sysconfig/${NAME} fi # Path to the tomcat launch script TOMCAT_SCRIPT="/opt/tomcat/bin/catalina.sh" # Tomcat program name TOMCAT_PROG="$NAME" # Define the tomcat username TOMCAT_USER="${TOMCAT_USER:-tomcat}" # Define the tomcat log file TOMCAT_LOG="${TOMCAT_LOG:-/var/log/tomcat/catalina.out}" RETVAL="0" # remove when the RHEL and FC daemon functions converge # (pulled from /etc/rc.d/init.d/functions) #function checkpid() { # local i # for i in $* ; do # if [ -d "/proc/${i}" ]; then # return 0 # fi # done # return 1 #} function parseOptions() { options="" options="$options $( awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \ $TOMCAT_CFG )" if [ -r "/etc/sysconfig/${NAME}" ]; then options="$options $( awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \ /etc/sysconfig/${NAME} )" fi TOMCAT_SCRIPT="$options $TOMCAT_SCRIPT" } # See how we were called. function start() { echo -n "Starting ${TOMCAT_PROG}: " if [ -f "/var/lock/subsys/${NAME}" ] ; then if [ -f "/var/run/${NAME}.pid" ]; then read kpid < /var/run/${NAME}.pid if checkpid $kpid 2>&1; then echo "$NAME process already running" return -1 else echo "lock file found but no process running for" echo "pid $kpid, continuing" fi fi fi export CATALINA_PID="/var/run/${NAME}.pid" touch $CATALINA_PID chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID #$SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT start" >> $TOMCAT_LOG 2>&1 daemon --user $TOMCAT_USER $TOMCAT_SCRIPT start >> $TOMCAT_LOG 2>&1 RETVAL="$?" if [ "$RETVAL" -eq 0 ]; then echo_success touch /var/lock/subsys/${NAME} else echo_failure fi echo return $RETVAL } function status() { RETVAL="1" if [ -f "/var/run/${NAME}.pid" ]; then read kpid < /var/run/${NAME}.pid if checkpid $kpid 2>&1; then echo "$0 is already running (${kpid})" RETVAL="0" else echo "lock file found but no process running for pid $kpid" fi else pid="$(pgrep -u tomcat java)" if [ -n "$pid" ]; then echo "$0 running (${pid}) but no PID file exists" RETVAL="0" else echo "$0 is stopped" fi fi return $RETVAL } function stop() { local STOP_VERBOSE="false" echo -n "Stopping $TOMCAT_PROG: " if [ -f "/var/lock/subsys/${NAME}" ]; then #$SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop" >> $TOMCAT_LOG 2>&1 daemon --user $TOMCAT_USER $TOMCAT_SCRIPT stop >> $TOMCAT_LOG 2>&1 RETVAL="$?" if [ "$RETVAL" -eq "0" ]; then count="0" if [ -f "/var/run/${NAME}.pid" ]; then read kpid < /var/run/${NAME}.pid until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \ [ "$count" -gt "$SHUTDOWN_WAIT" ]; do if [ "$STOP_VERBOSE" = "true" ]; then echo -n -e "\nwaiting for processes $kpid to exit" fi sleep 1 let count="${count}+1" done if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then if [ "$STOP_VERBOSE" = "true" ]; then echo -n -e "\nkilling processes which didn't stop" echo -n -e "after " echo -n "$SHUTDOWN_WAIT seconds" fi kill -9 $kpid fi echo_success if [ "$count" -gt "0" ]; then echo -n -e "\n" fi fi rm -f /var/lock/subsys/$NAME /var/run/$NAME.pid else echo_failure fi fi } # See how we were called. case "$1" in start) parseOptions start ;; stop) parseOptions stop ;; restart) parseOptions stop sleep 2 start ;; condrestart) if [ -f "/var/run/${NAME}.pid" ]; then parseOptions stop start fi ;; status) status ;; version) parseOptions "${JAVA_HOME}/bin/java" \ -classpath "${CATALINA_HOME}/server/lib/catalina.jar" \ org.apache.catalina.util.ServerInfo ;; *) echo "Usage: $TOMCAT_PROG {start|stop|restart|condrestart|status|version}" exit 1 esac exit $RETVAL