2008-04-07 22:37:26 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2011-04-28 05:35:45 +00:00
|
|
|
# tomcat This shell script takes care of starting and stopping Tomcat
|
2008-04-07 22:37:26 +00:00
|
|
|
#
|
|
|
|
# chkconfig: - 80 20
|
|
|
|
#
|
|
|
|
### BEGIN INIT INFO
|
2011-04-28 05:35:45 +00:00
|
|
|
# Provides: tomcat
|
2008-04-07 22:37:26 +00:00
|
|
|
# Required-Start: $network $syslog
|
|
|
|
# Required-Stop: $network $syslog
|
|
|
|
# Default-Start:
|
|
|
|
# Default-Stop:
|
2011-04-27 05:54:04 +00:00
|
|
|
# Description: Release implementation for Servlet 3.0 and JSP 2.2
|
2008-04-07 22:37:26 +00:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2008-10-13 20:15:33 +00:00
|
|
|
## Source function library.
|
|
|
|
#. /etc/rc.d/init.d/functions
|
|
|
|
# Source LSB function library.
|
|
|
|
if [ -r /lib/lsb/init-functions ]; then
|
|
|
|
. /lib/lsb/init-functions
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
DISTRIB_ID=`lsb_release -i -s 2>/dev/null`
|
2008-04-07 22:37:26 +00:00
|
|
|
|
|
|
|
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
|
2010-10-07 18:25:50 +00:00
|
|
|
SU="/sbin/runuser -s /bin/sh"
|
2008-04-07 22:37:26 +00:00
|
|
|
else
|
2010-10-07 18:25:50 +00:00
|
|
|
SU="/bin/su -s /bin/sh"
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
|
|
|
|
2011-01-17 19:01:02 +00:00
|
|
|
# Get the tomcat config (use this for environment specific settings)
|
2011-04-28 05:35:45 +00:00
|
|
|
TOMCAT_CFG="/etc/tomcat/tomcat.conf"
|
2011-01-17 19:01:02 +00:00
|
|
|
if [ -r "$TOMCAT_CFG" ]; then
|
|
|
|
. $TOMCAT_CFG
|
|
|
|
fi
|
|
|
|
|
2011-02-28 22:48:39 +00:00
|
|
|
# Get instance specific config file
|
|
|
|
if [ -r "/etc/sysconfig/${NAME}" ]; then
|
|
|
|
. /etc/sysconfig/${NAME}
|
|
|
|
fi
|
|
|
|
|
2008-04-07 22:37:26 +00:00
|
|
|
# Define which connector port to use
|
|
|
|
CONNECTOR_PORT="${CONNECTOR_PORT:-8080}"
|
|
|
|
|
|
|
|
# Path to the tomcat launch script
|
2011-04-28 05:35:45 +00:00
|
|
|
TOMCAT_SCRIPT="/usr/sbin/tomcat"
|
2008-04-07 22:37:26 +00:00
|
|
|
|
|
|
|
# Tomcat program name
|
|
|
|
TOMCAT_PROG="${NAME}"
|
|
|
|
|
|
|
|
# Define the tomcat username
|
|
|
|
TOMCAT_USER="${TOMCAT_USER:-tomcat}"
|
|
|
|
|
|
|
|
# Define the tomcat log file
|
2011-04-28 05:35:45 +00:00
|
|
|
TOMCAT_LOG="${TOMCAT_LOG:-/var/log/tomcat/catalina.out}"
|
2011-02-28 22:48:39 +00:00
|
|
|
|
2008-04-07 22:37:26 +00:00
|
|
|
|
|
|
|
RETVAL="0"
|
|
|
|
|
|
|
|
# Look for open ports, as the function name might imply
|
|
|
|
function findFreePorts() {
|
|
|
|
local isSet1="false"
|
|
|
|
local isSet2="false"
|
|
|
|
local isSet3="false"
|
|
|
|
local lower="8000"
|
|
|
|
randomPort1="0"
|
|
|
|
randomPort2="0"
|
|
|
|
randomPort3="0"
|
|
|
|
local -a listeners="( $(
|
|
|
|
netstat -ntl | \
|
|
|
|
awk '/^tcp/ {gsub("(.)*:", "", $4); print $4}'
|
|
|
|
) )"
|
|
|
|
while [ "$isSet1" = "false" ] || \
|
|
|
|
[ "$isSet2" = "false" ] || \
|
|
|
|
[ "$isSet3" = "false" ]; do
|
|
|
|
let port="${lower}+${RANDOM:0:4}"
|
|
|
|
if [ -z `expr " ${listeners[*]} " : ".*\( $port \).*"` ]; then
|
|
|
|
if [ "$isSet1" = "false" ]; then
|
|
|
|
export randomPort1="$port"
|
|
|
|
isSet1="true"
|
|
|
|
elif [ "$isSet2" = "false" ]; then
|
|
|
|
export randomPort2="$port"
|
|
|
|
isSet2="true"
|
|
|
|
elif [ "$isSet3" = "false" ]; then
|
|
|
|
export randomPort3="$port"
|
|
|
|
isSet3="true"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeHomeDir() {
|
|
|
|
if [ ! -d "$CATALINA_HOME" ]; then
|
|
|
|
echo "$CATALINA_HOME does not exist, creating"
|
|
|
|
if [ ! -d "/usr/share/${NAME}" ]; then
|
|
|
|
mkdir /usr/share/${NAME}
|
2011-04-28 05:35:45 +00:00
|
|
|
cp -pLR /usr/share/tomcat/* /usr/share/${NAME}
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
|
|
|
mkdir -p /var/log/${NAME} \
|
|
|
|
/var/cache/${NAME} \
|
|
|
|
/var/tmp/${NAME}
|
|
|
|
ln -fs /var/cache/${NAME} ${CATALINA_HOME}/work
|
|
|
|
ln -fs /var/tmp/${NAME} ${CATALINA_HOME}/temp
|
|
|
|
cp -pLR /usr/share/${NAME}/bin $CATALINA_HOME
|
|
|
|
cp -pLR /usr/share/${NAME}/conf $CATALINA_HOME
|
2011-04-28 05:35:45 +00:00
|
|
|
ln -fs /usr/share/java/tomcat ${CATALINA_HOME}/lib
|
|
|
|
ln -fs /usr/share/tomcat/webapps ${CATALINA_HOME}/webapps
|
2008-04-07 22:37:26 +00:00
|
|
|
chown ${TOMCAT_USER}:${TOMCAT_USER} /var/log/${NAME}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2010-07-01 20:55:12 +00:00
|
|
|
|
|
|
|
echo -n "Starting ${TOMCAT_PROG}: "
|
|
|
|
if [ "$RETVAL" != "0" ]; then
|
|
|
|
log_failure_msg
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ -f "/var/lock/subsys/${NAME}" ]; then
|
2008-04-07 22:37:26 +00:00
|
|
|
if [ -f "/var/run/${NAME}.pid" ]; then
|
|
|
|
read kpid < /var/run/${NAME}.pid
|
2008-10-13 20:15:33 +00:00
|
|
|
# if checkpid $kpid 2>&1; then
|
|
|
|
if [ -d "/proc/${kpid}" ]; then
|
|
|
|
log_success_msg
|
|
|
|
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
|
|
|
|
echo
|
|
|
|
fi
|
2008-04-07 22:37:26 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# fix permissions on the log and pid files
|
|
|
|
export CATALINA_PID="/var/run/${NAME}.pid"
|
2010-07-01 20:55:12 +00:00
|
|
|
touch $CATALINA_PID 2>&1 || RETVAL="4"
|
|
|
|
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then
|
|
|
|
chown ${TOMCAT_USER}:${TOMCAT_USER} $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_USER} $TOMCAT_LOG
|
|
|
|
fi
|
2011-04-28 05:35:45 +00:00
|
|
|
if [ "$CATALINA_HOME" != "/usr/share/tomcat" -a "$RETVAL" -eq "0" ]; then
|
2008-04-07 22:37:26 +00:00
|
|
|
# Create a tomcat directory if it doesn't exist
|
|
|
|
makeHomeDir
|
|
|
|
# If CATALINA_HOME doesn't exist modify port number so that
|
|
|
|
# multiple instances don't interfere with each other
|
|
|
|
findFreePorts
|
|
|
|
sed -i -e "s/8005/${randomPort1}/g" -e "s/8080/${CONNECTOR_PORT}/g" \
|
|
|
|
-e "s/8009/${randomPort2}/g" -e "s/8443/${randomPort3}/g" \
|
|
|
|
${CATALINA_HOME}/conf/server.xml
|
|
|
|
fi
|
|
|
|
parseOptions
|
2010-07-01 20:55:12 +00:00
|
|
|
if [ "$RETVAL" -eq "0" -a "$SECURITY_MANAGER" = "true" ]; then
|
2008-04-07 22:37:26 +00:00
|
|
|
$SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start-security" \
|
2011-04-13 19:15:12 +00:00
|
|
|
>> /var/log/${NAME}/initd.log 2>&1 || RETVAL="4"
|
2008-04-07 22:37:26 +00:00
|
|
|
else
|
2010-07-01 20:55:12 +00:00
|
|
|
|
2011-04-13 19:15:12 +00:00
|
|
|
[ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start" >> /var/log/${NAME}/initd.log 2>&1 || RETVAL="4"
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
2010-07-01 20:55:12 +00:00
|
|
|
if [ "$RETVAL" -eq "0" ]; then
|
2008-10-13 20:15:33 +00:00
|
|
|
log_success_msg
|
2008-04-07 22:37:26 +00:00
|
|
|
touch /var/lock/subsys/${NAME}
|
|
|
|
else
|
2010-07-01 20:55:12 +00:00
|
|
|
log_failure_msg "Error code ${RETVAL}"
|
2008-10-13 20:15:33 +00:00
|
|
|
fi
|
|
|
|
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
|
|
|
|
echo
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop() {
|
|
|
|
echo -n "Stopping ${TOMCAT_PROG}: "
|
|
|
|
if [ -f "/var/lock/subsys/${NAME}" ]; then
|
2010-07-01 20:55:12 +00:00
|
|
|
parseOptions
|
|
|
|
if [ "$RETVAL" -eq "0" ]; then
|
|
|
|
touch /var/lock/subsys/${NAME} 2>&1 || RETVAL="4"
|
2011-04-13 19:15:12 +00:00
|
|
|
[ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} stop" >> /var/log/${NAME}/initd.log 2>&1 || RETVAL="4"
|
2010-07-01 20:55:12 +00:00
|
|
|
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" ] || \
|
2008-04-07 22:37:26 +00:00
|
|
|
[ "$count" -gt "$SHUTDOWN_WAIT" ]; do
|
|
|
|
if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
|
|
|
|
echo "waiting for processes $kpid to exit"
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
let count="${count}+1"
|
|
|
|
done
|
|
|
|
if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
|
|
|
|
if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
|
2010-07-01 20:55:12 +00:00
|
|
|
log_warning_msg "killing processes which did not stop after ${SHUTDOWN_WAIT} seconds"
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
|
|
|
kill -9 $kpid
|
|
|
|
fi
|
2008-10-13 20:15:33 +00:00
|
|
|
log_success_msg
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
|
|
|
rm -f /var/lock/subsys/${NAME} /var/run/${NAME}.pid
|
|
|
|
else
|
2008-10-13 20:15:33 +00:00
|
|
|
log_failure_msg
|
2010-07-01 20:55:12 +00:00
|
|
|
RETVAL="4"
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
|
|
|
else
|
2008-10-13 20:15:33 +00:00
|
|
|
log_success_msg
|
2010-07-01 20:55:12 +00:00
|
|
|
RETVAL="0"
|
2008-10-13 20:15:33 +00:00
|
|
|
fi
|
|
|
|
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
|
|
|
|
echo
|
2008-04-07 22:37:26 +00:00
|
|
|
fi
|
2010-07-01 20:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function usage()
|
|
|
|
{
|
|
|
|
echo "Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}"
|
|
|
|
RETVAL="2"
|
2008-04-07 22:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# See how we were called.
|
2010-07-01 20:55:12 +00:00
|
|
|
RETVAL="0"
|
2008-04-07 22:37:26 +00:00
|
|
|
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)
|
|
|
|
if [ -f "/var/run/${NAME}.pid" ]; then
|
2008-10-13 20:15:33 +00:00
|
|
|
read kpid < /var/run/${NAME}.pid
|
|
|
|
if [ -d "/proc/${kpid}" ]; then
|
2010-07-01 20:55:12 +00:00
|
|
|
log_success_msg "${NAME} (pid ${kpid}) is running..."
|
2008-10-13 20:15:33 +00:00
|
|
|
RETVAL="0"
|
2010-07-01 20:55:12 +00:00
|
|
|
else
|
|
|
|
# The pid file exists but the process is not running
|
|
|
|
log_warning_msg "PID file exists, but process is not running"
|
|
|
|
RETVAL="1"
|
2008-10-13 20:15:33 +00:00
|
|
|
fi
|
2008-04-07 22:37:26 +00:00
|
|
|
else
|
2008-10-13 20:15:33 +00:00
|
|
|
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)"
|
2008-04-07 22:37:26 +00:00
|
|
|
if [ -z "$pid" ]; then
|
2008-10-13 20:15:33 +00:00
|
|
|
# status ${NAME}
|
|
|
|
# RETVAL="$?"
|
2010-07-01 20:55:12 +00:00
|
|
|
log_success_msg "${NAME} is stopped"
|
2008-10-13 20:15:33 +00:00
|
|
|
RETVAL="3"
|
2008-04-07 22:37:26 +00:00
|
|
|
else
|
2010-07-01 20:55:12 +00:00
|
|
|
log_success_msg "${NAME} (pid $pid) is running..."
|
2008-04-07 22:37:26 +00:00
|
|
|
RETVAL="0"
|
|
|
|
fi
|
|
|
|
fi
|
2010-07-01 20:55:12 +00:00
|
|
|
if [ -f /var/lock/subsys/${NAME} ]; then
|
|
|
|
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)"
|
|
|
|
# The lockfile exists but the process is not running
|
|
|
|
if [ -z "$pid" ]; then
|
|
|
|
log_failure_msg "${NAME} lockfile exists but process is not running"
|
|
|
|
RETVAL="2"
|
|
|
|
fi
|
|
|
|
fi
|
2008-04-07 22:37:26 +00:00
|
|
|
;;
|
|
|
|
version)
|
|
|
|
${TOMCAT_SCRIPT} version
|
|
|
|
;;
|
|
|
|
*)
|
2010-07-01 20:55:12 +00:00
|
|
|
usage
|
|
|
|
;;
|
2008-04-07 22:37:26 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
exit $RETVAL
|