#!/bin/bash
#
# tomcat6 This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat6
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Release implementation for Servlet 2.5 and JSP 2.1
# Short-Description: start and stop tomcat
### END INIT INFO
#
# - originally written by Henri Gomez, Keith Irwin, and Nicolas Mailhot
# - heavily rewritten by Deepak Bhole and Jason Corley
#
## Source function library.
#. /etc/rc.d/init.d/functions
# Source LSB function library.
#如果/lib/lsb/init-functions存在且可读
if [ -r /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
else
exit 1
fi
# Define which connector port to use
CONNECTOR_PORT="8022"
# Path to the tomcat launch script
TOMCAT_SCRIPT="/app/apache-tomcat-6.0.33/bin/tomcatBoot.sh"
# Define the tomcat username
TOMCAT_USER="betadmin"
TOMCAT_GROUP="bet"
# Define the tomcat log file
TOMCAT_LOG="/app/apache-tomcat-6.0.33/logs/aaa.out"
# Tomcat program name
NAME="$(basename $0)"
TOMCAT_PROG="${NAME}"
# Set the TOMCAT_PID location
CATALINA_PID="/var/run/${NAME}.pid"
# Time to wait in seconds, before killing process
SHUTDOWN_WAIT="30"
RETVAL="0"
# See how we were called.
function start() {
echo -n "Starting ${TOMCAT_PROG}: "
if [ "$RETVAL" != "0" ]; then
log_failure_msg
return
fi
if [ -f "/var/lock/subsys/${NAME}" ]; then
if [ -f "/var/run/${NAME}.pid" ]; then
read kpid < /var/run/${NAME}.pid
if [ -d "/proc/${kpid}" ]; then
log_success_msg
RETVAL="0"
return
fi
fi
fi
# fix permissions on the log and pid files
export CATALINA_PID="/var/run/${NAME}.pid"
touch $CATALINA_PID 2>&1 || RETVAL="4"
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then
chown ${TOMCAT_USER}:${TOMCAT_GROUP} $CATALINA_PID
fi
[ "$RETVAL" -eq "0" ] && touch $TOMCAT_LOG 2>&1 || RETVAL="4"
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then
chown ${TOMCAT_USER}:${TOMCAT_GROUP} $TOMCAT_LOG
fi
[ "$RETVAL" -eq "0" ] && su - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start ${TOMCAT_LOG}" >> $TOMCAT_LOG 2>&1 || RETVAL="4"
if [ "$RETVAL" -eq "0" ]; then
log_success_msg
echo "$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_GROUP} java)" >> $CATALINA_PID
touch /var/lock/subsys/${NAME}
else
log_failure_msg "Error code ${RETVAL}"
fi
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
echo
fi
}
function stop() {
echo -n "Stopping ${TOMCAT_PROG}: "
if [ -f "/var/lock/subsys/${NAME}" ]; then
if [ "$RETVAL" -eq "0" ]; then
touch /var/lock/subsys/${NAME} 2>&1 || RETVAL="4"
[ "$RETVAL" -eq "0" ] && su - $TOMCAT_USER -c "${TOMCAT_SCRIPT} stop ${TOMCAT_LOG}" >> $TOMCAT_LOG 2>&1 || RETVAL="4"
fi
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
sleep 1
let count="${count}+1"
done
if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
kill -9 $kpid
fi
log_success_msg
fi
rm -f /var/lock/subsys/${NAME} /var/run/${NAME}.pid
else
log_failure_msg
RETVAL="4"
fi
else
log_success_msg
RETVAL="0"
fi
}
function status()
{
checkpidfile
if [ "$RETVAL" -eq "0" ]; then
log_success_msg "${NAME} (pid ${kpid}) is running..."
elif [ "$RETVAL" -eq "1" ]; then
log_failure_msg "PID file exists, but process is not running"
else
checklockfile
if [ "$RETVAL" -eq "2" ]; then
log_failure_msg "${NAME} lockfile exists but process is not running"
else
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_GROUP} java)"
if [ -z "$pid" ]; then
log_success_msg "${NAME} is stopped"
RETVAL="3"
else
log_success_msg "${NAME} (pid $pid) is running..."
RETVAL="0"
fi
fi
fi
}
function checklockfile()
{
if [ -f /var/lock/subsys/${NAME} ]; then
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_GROUP} java)"
# The lockfile exists but the process is not running
if [ -z "$pid" ]; then
RETVAL="2"
fi
fi
}
function checkpidfile()
{
if [ -f "/var/run/${NAME}.pid" ]; then
read kpid < /var/run/${NAME}.pid
if [ -d "/proc/${kpid}" ]; then
# The pid file exists and the process is running
RETVAL="0"
else
# The pid file exists but the process is not running
RETVAL="1"
return
fi
fi
# pid file does not exist and program is not running
RETVAL="3"
}
function usage()
{
echo "Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}"
RETVAL="2"
}
# See how we were called.
RETVAL="0"
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart|try-restart)
if [ -f "/var/run/${NAME}.pid" ]; then
stop
start
fi
;;
reload)
RETVAL="3"
;;
force-reload)
if [ -f "/var/run/${NAME}.pid" ]; then
stop
start
fi
;;
status)
status
;;
version)
${TOMCAT_SCRIPT} version
;;
*)
usage
;;
esac
exit $RETVAL
#!/bin/bash
# Where your tomcat installation lives
CATALINA_BASE="/app/apache-tomcat-6.0.33"
CATALINA_HOME="/app/apache-tomcat-6.0.33"
JASPER_HOME="/app/apache-tomcat-6.0.33"
CATALINA_TMPDIR="/app/apache-tomcat-6.0.33/temp"
JAVA_HOME=/usr/java/jdk1.6.0_43/bin
export JAVA_HOME
set_jvm() {
# use $JAVA_HOME if defined
if [ -n "$JAVA_HOME" ] ; then
return
fi
# try to find it from java command
# try javac first, or we might get the location of the jre instead - djw
java=`which javac 2>/dev/null || :`
# if we don't have the jdk, then maybe we have the jre - djw
if [ -z "$java" ] ; then
java=`which java 2>/dev/null || :`
fi
if [ -n "$java" ] ; then
while [ -h "$java" ] ; do
java=`readlink $java 2>/dev/null`
done
JAVA_HOME="`dirname $java`/.."
export JAVA_HOME
return
fi
echo "Can't find java virtual machine, aborting."
exit 1
}
set_javacmd() {
if [ -x "$JAVACMD" ]; then
return
fi
set_jvm
# Add all sorts of jvm layouts here
if [ -x "$JAVA_HOME/jre/sh/java" ]; then
JAVACMD="$JAVA_HOME/jre/sh/java"
elif [ -x "$JAVA_HOME/bin/java" ]; then
JAVACMD="$JAVA_HOME/bin/java"
else
JAVACMD=`which java 2>/dev/null`
fi
if [ ! -x "$JAVACMD" ]; then
echo "$0: error: Failed to set JAVACMD"
return 1
fi
if [ -n "$JAVACMD_OPTS" ]; then
JAVACMD="$JAVACMD $JAVACMD_OPTS"
fi
return 0
}
set_javacmd
# CLASSPATH munging
if [ -n "$JSSE_HOME" ]; then
CLASSPATH="${CLASSPATH}:$(build-classpath jcert jnet jsse 2>/dev/null)"
fi
CLASSPATH="${CLASSPATH}:${CATALINA_HOME}/bin/bootstrap.jar"
CLASSPATH="${CLASSPATH}:${CATALINA_HOME}/bin/tomcat-juli.jar"
CLASSPATH="${CLASSPATH}:$(build-classpath commons-daemon 2>/dev/null)"
if [ "$1" = "start" ]; then
${JAVACMD} $JAVA_OPTS $CATALINA_OPTS \
-classpath "$CLASSPATH" \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
-Djava.util.logging.config.file="${CATALINA_BASE}/conf/logging.properties" \
-Djava.util.logging.manager="org.apache.juli.ClassLoaderLogManager" \
org.apache.catalina.startup.Bootstrap start \
>> ${2} 2>&1 &
if [ ! -z "$CATALINA_PID" ]; then
echo $! > $CATALINA_PID
fi
elif [ "$1" = "start-security" ]; then
${JAVACMD} $JAVA_OPTS $CATALINA_OPTS \
-classpath "$CLASSPATH" \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
-Djava.security.manager \
-Djava.security.policy=="${CATALINA_BASE}/conf/catalina.policy" \
-Djava.util.logging.config.file="${CATALINA_BASE}/conf/logging.properties" \
-Djava.util.logging.manager="org.apache.juli.ClassLoaderLogManager" \
org.apache.catalina.startup.Bootstrap start \
>> ${2} 2>&1 &
if [ ! -z "$CATALINA_PID" ]; then
echo $! > $CATALINA_PID
fi
elif [ "$1" = "stop" ]; then
${JAVACMD} $JAVA_OPTS \
-classpath "$CLASSPATH" \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
org.apache.catalina.startup.Bootstrap stop \
>> ${2} 2>&1
elif [ "$1" = "version" ]; then
${JAVACMD} -classpath ${CATALINA_HOME}/lib/catalina.jar \
org.apache.catalina.util.ServerInfo
else
echo "Usage: $0 {start|start-security|stop|version}"
exit 1
fi