Skip to content

Instantly share code, notes, and snippets.

@galak-fyyar
Created July 6, 2014 22:04
Show Gist options
  • Save galak-fyyar/e57ad863e3286eea4f15 to your computer and use it in GitHub Desktop.
Save galak-fyyar/e57ad863e3286eea4f15 to your computer and use it in GitHub Desktop.
/etc/init.d/tomcat
#!/bin/sh
#
# tomcat70 Startup script for Tomcat 7.0, the Apache Servlet Engine
#
#
# chkconfig: - 80 16
# description: Tomcat 7.0 is the Apache Servlet Engine
# processname: tomcat70
# config: /etc/tomcat/tomcat70.conf
# pidfile: /var/run/tomcat70.pid
#
# Gomez Henri <hgomez@users.sourceforge.net>
# Keith Irwin <keith_irwin@non.hp.com>
# Nicolas Mailhot <nicolas.mailhot@one2team.com>
#
# Adapted and extended for Tomcat6, Tomcat7 default installation on CentOS by Robert Oschwald <robertoschwald@google****.com>
# Note: You must create a config file /etc/tomcat/tomcat70.conf
# version 1.02 - Removed initlog support
# version 1.03 - Removed config:
# version 1.04 - tomcat will start before httpd and stop after httpd
# version 1.05 - jdk hardcoded to link /usr/java/jdk and tomcat runs as "nobody"
# version 1.06 - split up into script and config file
# version 1.07 - Rework from Nicolas ideas
# version 1.08 - Fix work dir permission at start time, switch to use tomcat4
# version 1.09 - Fix pidfile and config tags
# version 1.10 - Fallback to su direct use on systems without Redhat/Mandrake init.d functions
# version 1.11 - Fix webapps dir permissions
# version 1.12 - remove initial start/stop level for chkconfig (- 80 20)
# version 1.13 - remove chown of logs/work/temp/webapps dir, owned by tomcat4 at install time
# version 1.14 - correct the start/stop ugly hack by waiting all the threads stops
# version 1.15 - ensure we're looking for TOMCAT_USER running catalina
# version 1.16 - Add support for CATALINA_PID env var
# version 1.17 - Remove run files only tomcat started correctl
# in start area, check that tomcat is not allready running
# version 1.18 - Fix kill typo (thanks Kaj J. Niemi)
# version 1.19 - Add jar relinking
# version 1.20 - Check there is no stalling tomcat4.pid
# version 1.20tc5 - Changed all instances of tomcat4 to tomcat5 except TOMCAT_USER
# version 1.21 - Add status command
# version 1.21a - Adapted for tomcat55 manual installation, roos
# version 1.21b - tomcat55.conf handling added, roos
# version 1.21b-a - moved tomcat55.conf
# version 1.21b-b - some more changes due to multi-tomcat boot
# version 1.21b-c - tomcat7 handling added, roos
#
# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
NAME="$(basename $0)"
# Get Tomcat config
TOMCAT_CFG="/opt/ibooking-stable/etc/tomcat70.conf"
[ -r "$TOMCAT_CFG" ] && . "${TOMCAT_CFG}"
# if CATALINA_HOME is not set, boil out
if [ -z "$CATALINA_HOME" ]; then
echo "Fatal! CATALINA_HOME not set in tomcat70.conf"
exit 1
fi
# Path to the tomcat launch script (direct don't use wrapper)
TOMCAT_SCRIPT=$CATALINA_HOME/bin/catalina.sh
# by roos, propper umask
umask 002
# if TOMCAT_USER is not set, use tomcat like Apache HTTP server
TOMCAT_USER="${TOMCAT_USER:-tomcat}"
TOMCAT_LOCK_FILE="${TOMCAT_LOCK_FILE:-/var/lock/subsys/tomcat70}"
# Since the daemon function will sandbox $tomcat
# no environment stuff should be defined here anymore.
# Please use the tomcat.conf file instead ; it will
# be read by the $tomcat script
RETVAL=0
# added by roos to be able to delete logfiles
umask 002
# See how we were called.
start() {
echo -n "Starting $NAME: "
if [ -f "$TOMCAT_LOCK_FILE" ] ; then
if [ -f $CATALINA_PID ]; then
read kpid < $CATALINA_PID
# if checkpid $kpid 2>&1; then
if [ -d "/proc/${kpid}" ]; then
echo "process allready running"
return -1
else
echo "lock file found but no process running for pid $kpid, continuing"
fi
fi
fi
touch $CATALINA_PID
chmod 0777 $CATALINA_PID
chown $TOMCAT_USER $CATALINA_PID
if [ -x /etc/rc.d/init.d/functions ]; then
daemon --user $TOMCAT_USER $TOMCAT_SCRIPT start
else
su - $TOMCAT_USER -c "$TOMCAT_SCRIPT start"
fi
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $TOMCAT_LOCK_FILE
return $RETVAL
}
function stop() {
RETVAL="0"
echo -n "Stopping ${NAME}: "
if [ -f "$TOMCAT_LOCK_FILE" ]; then
if [ -x /etc/rc.d/init.d/functions ]; then
daemon --user $TOMCAT_USER $TOMCAT_SCRIPT stop
else
su - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop"
fi
RETVAL=$?
if [ "$RETVAL" -eq "0" ]; then
count="0"
if [ -f $CATALINA_PID ]; then
read kpid < $CATALINA_PID
if [ -z $kpid ]; then
# already stopped
rm -f $TOMCAT_LOCK_FILE $CATALINA_PID
return $RETVAL
fi
until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
[ "$count" -gt "$SHUTDOWN_WAIT" ]; do
echo "waiting for processes $kpid to exit"
sleep 1
let count="${count}+1"
done
if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
echo "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $kpid
fi
fi
rm -f $TOMCAT_LOCK_FILE $CATALINA_PID
fi
fi
return $RETVAL
}
status() {
if [ -f "${TOMCAT_LOCK_FILE}" ] ; then
if [ -f $CATALINA_PID ]; then
read kpid < $CATALINA_PID
if checkpid $kpid 2>&1; then
echo "tomcat70 is running ($kpid)"
return 0
else
echo "lock file found but no process running for pid $kpid"
fi
fi
fi
echo "${NAME} is stopped."
return 1
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
exit $?
;;
restart)
stop
sleep 2
start
;;
condrestart)
if [ -f $CATALINA_PID ] ; then
stop
start
fi
;;
*)
echo "Usage: $NAME {start|stop|status|restart|condrestart}"
exit 1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment