Skip to content

Instantly share code, notes, and snippets.

@SeonghoonKim
Created October 25, 2012 08:36
Show Gist options
  • Save SeonghoonKim/3951447 to your computer and use it in GitHub Desktop.
Save SeonghoonKim/3951447 to your computer and use it in GitHub Desktop.
Tomcat Init Script for Linux
#! /bin/sh
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: tomcat init script
# Description: tomcat init script
### END INIT INFO
#CATALINA_HOME=/opt/apache-tomcat-7.0.29
RUN_AS_USER=tomcat
#PID_FILE=/var/run/tomcat.pid
SCRIPT="$0"
# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path.
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
# Drop everything prior to ->
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done
SCRIPT_DIR=`dirname "$SCRIPT"`
# Only set CATALINA_HOME if not already set
[ -z "$TOMCAT_HOME" ] && TOMCAT_HOME=`cd "$SCRIPT_DIR/.." >/dev/null; pwd`
# Only set PID_FILE if not already set
[ -z "$PID_FILE" ] && PID_FILE=`cd "$SCRIPT_DIR" >/dev/null; pwd`/tomcat.pid
export CATALINA_PID="$PID_FILE"
TOMCAT_SCRIPT="$TOMCAT_HOME/bin/catalina.sh"
# Check for tomcat script
if [ ! -x "$TOMCAT_SCRIPT" ]; then
echo "Cannot find $TOMCAT_SCRIPT"
echo "The file is absent or does not have execute permission"
exit 1
fi
start() {
if [ -f "/sbin/runuser" ]; then
/sbin/runuser $RUN_AS_USER -s /bin/sh -c "$TOMCAT_SCRIPT start"
else
su $RUN_AS_USER -s /bin/sh -c "$TOMCAT_SCRIPT start"
fi
RETVAL=$?
}
stop() {
if [ -f "/sbin/runuser" ]; then
/sbin/runuser $RUN_AS_USER -s /bin/sh -c "$TOMCAT_SCRIPT stop"
else
su $RUN_AS_USER -s /bin/sh -c "$TOMCAT_SCRIPT stop"
fi
RETVAL=$?
}
restart() {
stop
sleep 1
start
}
status() {
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Not running, but PID_FILE exists"
else
echo "Running ($PID)"
fi
else
echo "Not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment