Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djechelon/e0a6e2268a7e6ab34e1711401c42754a to your computer and use it in GitHub Desktop.
Save djechelon/e0a6e2268a7e6ab34e1711401c42754a to your computer and use it in GitHub Desktop.
Apache Tomcat init script (or startup/controll script). Works fine for version 7/8. Read the comments for release history. Feel free to modify, copy and give suggestions. (c) GNU General Public License
#!/bin/bash
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO
#
# description: Apache Tomcat init script
# processname: tomcat
# chkconfig: 234 20 80
#
#
# Copyright (C) 2014 Miglen Evlogiev
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
# Initially forked from: gist.github.com/valotas/1000094
# Source: gist.github.com/miglen/5590986
RED="\e[00;31m"
GREEN="\e[00;32m"
BLACK="\e[00m"
#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/java/latest
#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH
JAVA_OPTS=
#JAVA_OPTS="-server -Xms256m -Xmx256M -XX:MaxPermSize=512m"
#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/opt/apache-tomcat
#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/opt/apache-tomcat
#TOMCAT_USER is the default user of tomcat, must be specified, enter non-existing user to always run as current user
export TOMCAT_USER=ott
#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {${GREEN}start${BLACK}|${RED}stop${BLACK}|${RED}kill${BLACK}|${GREEN}status${BLACK}|${RED}restart${BLACK}}"
#SHUTDOWN_WAIT is wait time in seconds for tomcat java process to stop
SHUTDOWN_WAIT=20
KILL_ON_HANGING_SHUTDOWN=false
tomcat_pid() {
# Org before alex edit:
# echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
echo `ps -fe | grep "catalina.base=$CATALINA_BASE" | grep -v grep | tr -s " "|cut -d" " -f2`
}
waitForStart() {
# Check curl available
which curl > /dev/null
whichRes=$?
# Check curl for lib-problem (code 127)
curl 2> /dev/null
curlRes=$?
if (( whichRes == 0 && curlRes != 127 )) ; then
until [ "`curl --silent --show-error --connect-timeout 1 -I http://localhost:8080 2>&1 | grep 'Coyote'`" != "" ];
do
echo "--- sleeping for 1 seconds"
sleep 1
done
echo -e "${GREEN}Tomcat is ready!${BLACK}"
fi
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo -e "${RED}Tomcat is already running (pid: $pid)${BLACK}"
else
# Start tomcat
echo -e "${GREEN}Starting tomcat${BLACK}"
#ulimit -n 100000
#umask 007
#/bin/su -p -s /bin/sh $TOMCAT_USER
# If $TOMCAT_USER is specified and exists we always log in as that user
if [ `user_exists $TOMCAT_USER` = "1" ]
then
# su requires login even if login to myself
#/bin/su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
# -H to get Home of $TOMCAT_USER, -E to keep environment we have set here, such as JAVA_HOME
sudo -H -E -u $TOMCAT_USER $CATALINA_HOME/bin/startup.sh $JAVA_OPTS
else
sh $CATALINA_HOME/bin/startup.sh $JAVA_OPTS
fi
waitForStart
status
fi
return 0
}
status(){
pid=$(tomcat_pid)
if [ -n "$pid" ] ; then
echo -e "${GREEN}Tomcat is running with pid: $pid${BLACK}"
else
echo -e "${RED}Tomcat is not running${BLACK}"
return 3
fi
}
terminate() {
pid=$(tomcat_pid)
if [ -n "$pid" ]; then
echo -e "${RED}Terminating Tomcat with pid=$pid${BLACK}"
kill -9 $pid
else
echo -e "${RED}Tomcat is not running${BLACK}"
fi
}
execStop() {
if [ -z "$stop_exec_success" ]; then
out=$($CATALINA_HOME/bin/shutdown.sh 2>&1)
echo
echo "$out"
isConnectionRefused=$(echo "$out" | grep "Connection refused")
if [ -z "$isConnectionRefused" ];
then
stop_exec_success="true"
fi
fi
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo -e "${RED}Stopping Tomcat with PID=$pid${BLACK}"
execStop
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\n${RED}waiting for processes to exit${BLACK}";
sleep 1
# multiple execStop is probably not necessary if start script waits for start
execStop
let count=$count+1;
done
echo
if [ $count -gt $kwait ]; then
if [ "$KILL_ON_HANGING_SHUTDOWN" = "true" ] ; then
echo -e "\n${RED}killing processes didn't stop after $SHUTDOWN_WAIT seconds${BLACK}"
terminate
else
echo -e "\n${RED}Process didn't stop after $SHUTDOWN_WAIT seconds${BLACK}"
exit 1
fi
fi
else
echo -e "${RED}Tomcat is not running${BLACK}"
fi
return 0
}
user_exists(){
if id -u $1 >/dev/null 2>&1; then
echo "1"
else
echo "0"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
exit $?
;;
kill)
terminate
;;
*)
echo -e $TOMCAT_USAGE
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment