Skip to content

Instantly share code, notes, and snippets.

@joshuabaird
Created July 21, 2015 15:16
Show Gist options
  • Save joshuabaird/560e59f5f63c748c7630 to your computer and use it in GitHub Desktop.
Save joshuabaird/560e59f5f63c748c7630 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO
# Source LSB function library.
if [ -r /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
else
exit 1
fi
NAME="$(basename $0)"
# Get the tomcat config (use this for environment specific settings)
TOMCAT_CFG="/opt/tomcat/conf/tomcat.conf"
if [ -r "$TOMCAT_CFG" ]; then
. $TOMCAT_CFG
fi
# For SELinux, we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
SU="/sbin/runuser -s /bin/sh"
else
SU="/bin/su -s /bin/sh"
fi
# Get instance specific config file
if [ -r "/etc/sysconfig/${NAME}" ]; then
. /etc/sysconfig/${NAME}
fi
# Define the Tomcat program name
TOMCAT_PROG="${NAME}"
# Define the Tomcat log
TOMCAT_LOG="${TOMCAT_LOG:-/var/log/${TOMCAT_PROG}/initd.log}"
tomcat_pid() {
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} -f Dcatalina.base=${CATALINA_BASE})"
echo ${pid}
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
log_failure_msg "${TOMCAT_PROG} is already running (pid: $pid)"
else
# Start tomcat
echo -n "Starting ${TOMCAT_PROG}: "
$SU - ${TOMCAT_USER} -c "export CATALINA_BASE=$CATALINA_BASE; cd $CATALINA_HOME/bin && $CATALINA_HOME/bin/startup.sh" >> ${TOMCAT_LOG} 2>&1
log_success_msg
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
# Stop tomcat
echo -n "Stoping ${TOMCAT_PROG}: "
$SU - ${TOMCAT_USER} -c "export CATALINA_BASE=$CATALINA_BASE; cd $CATALINA_HOME/bin && $CATALINA_HOME/bin/shutdown.sh" >> ${TOMCAT_LOG} 2>&1
log_success_msg
let kwait=$SHUTDOWN_WAIT
count=0
count_by=5
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
sleep $count_by
let count=$count+$count_by;
done
if [ $count -gt $kwait ]; then
echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment