Last active
May 7, 2019 21:24
-
-
Save atomicsamurai/684e8be27a1e35c2272ad40415f92bcb to your computer and use it in GitHub Desktop.
Tomcat management script - can manage multiple tomcat instances under a common folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Tomcat 7/8 start/stop/status script | |
| # Forked from: https://gist.github.com/miglen/5590986 | |
| # @author: Sandeep Chaturvedi <[email protected]> | |
| # | |
| # Release updates: | |
| # Updated to handle multiple tomcat instances under one directory | |
| # 'status' argument without any other argument will print all instances' status | |
| # any argument with the name of the tomcat instance (the subdirectory name under the common directory) will operate on that instance | |
| # Updated method for gathering pid of the current proccess | |
| # Added usage of CATALINA_BASE | |
| # Added coloring and additional status | |
| # Added check for existence of the tomcat user | |
| #set -x | |
| #Location of JAVA_HOME (bin files) | |
| #export JAVA_HOME=/etc/alternatives/java_sdk | |
| #Add Java binary files to PATH | |
| #export PATH=$JAVA_HOME/bin:$PATH | |
| #CATALINA_HOME is the location of the bin files of Tomcat | |
| #export CATALINA_HOME=/opt/forgerock/tomcat/openam | |
| # this is the directory under which different tomcat instances are | |
| SCRIPT_TOMCAT_BASE=/opt/tomcats | |
| # this is the name of the instance passed as argument | |
| TOMCAT_NAME=$2 | |
| # this is a list of all tomcat instances in SCRIPT_TOMCAT_BASE | |
| TOMCAT_NAMES=($(ls $SCRIPT_TOMCAT_BASE)) | |
| #CATALINA_BASE is the location of the configuration files of this instance of Tomcat | |
| CATALINA_BASE=/opt/tomcats/$TOMCAT_NAME | |
| #TOMCAT_USER is the default user of tomcat | |
| #export TOMCAT_USER=forgerock | |
| #SHUTDOWN_PORT is the default user of tomcat | |
| #export SHUTDOWN_PORT=8005 | |
| #TOMCAT_USAGE is the message if this script is called without any options | |
| TOMCAT_USAGE="Usage: `basename $0` {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m} <instance name> | `basename $0` {\e[00;32mstatus\e[00m|list}" | |
| #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop | |
| SHUTDOWN_WAIT=20 | |
| ALL_PIDS=() | |
| tomcat_pid() { | |
| echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "| cut -d" " -f2` | |
| } | |
| start() { | |
| pid=$(tomcat_pid) | |
| if [ -n "$pid" ] | |
| then | |
| echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m" | |
| else | |
| # Start tomcat | |
| echo -e "\e[00;32mStarting $TOMCAT_NAME tomcat\e[00m" | |
| #ulimit -n 100000 | |
| #umask 007 | |
| #/bin/su -p -s /bin/sh tomcat | |
| sh $CATALINA_BASE/bin/startup.sh | |
| echo -n -e "\n\e[00;32mwaiting for tomcat $TOMCAT_NAME to start\e[00m"; | |
| SERVER_PORT_STRING=`grep "Server port" $CATALINA_BASE/conf/server.xml | cut -d$'=' -f2 | cut -d$' ' -f1` | |
| SERVER_PORT=`echo ${SERVER_PORT_STRING//\"}` | |
| RET=`netstat -ltn | grep -c $SERVER_PORT` | |
| until [ $RET = '1' ]; do | |
| #until [ `curl -m 2 http://localhost:$HTTP_PORT 2>/dev/null | grep -c html` != '0' ] | |
| echo -n -e "\e[00;32m.\e[00m"; | |
| sleep 1 | |
| RET=`netstat -ltn | grep -c $SERVER_PORT` | |
| done | |
| status $TOMCAT_NAME | |
| fi | |
| return 0 | |
| } | |
| status(){ | |
| if [ "$1" = "all" ]; then | |
| for name in "${TOMCAT_NAMES[@]}"; do | |
| pid=`ps -fe | grep -E "$SCRIPT_TOMCAT_BASE/$name" | grep -v grep | tr -s " "| cut -d" " -f2` | |
| if [ -n "$pid" ]; then | |
| echo -e "\e[00;32mTomcat $name is running with pid: $pid\e[00m" | |
| else | |
| echo -e "\e[00;31mTomcat $name is not running\e[00m" | |
| fi | |
| done | |
| else | |
| pid=$(tomcat_pid) | |
| if [ -n "$pid" ]; then | |
| echo -e "\e[00;32mTomcat $TOMCAT_NAME is running with pid: $pid\e[00m" | |
| else | |
| echo -e "\e[00;31mTomcat is not running\e[00m" | |
| fi | |
| fi | |
| } | |
| stop() { | |
| pid=$(tomcat_pid) | |
| if [ -n "$pid" ]; then | |
| echo -e "\e[00;31mStoping Tomcat $TOMCAT_NAME \e[00m" | |
| #/bin/su -p -s /bin/sh tomcat | |
| sh $CATALINA_BASE/bin/shutdown.sh | |
| let kwait=$SHUTDOWN_WAIT | |
| count=0; | |
| echo -n -e "\n\e[00;31mwaiting for tomcat $TOMCAT_NAME to stop\e[00m"; | |
| until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ] | |
| do | |
| echo -n -e "\e[00;31m.\e[00m"; | |
| sleep 1 | |
| let count=$count+1; | |
| done | |
| if [ $count -gt $kwait ]; then | |
| echo -n -e "\n\e[00;31mkilling processes $pid which didn't stop after $SHUTDOWN_WAIT seconds\e[00m" | |
| kill -9 $pid | |
| fi | |
| else | |
| echo -e "\e[00;31mTomcat $TOMCAT_NAME is not running\e[00m" | |
| fi | |
| echo -n -e "\e[00;31mDone\n\e[00m"; | |
| return 0 | |
| } | |
| user_exists(){ | |
| if id -u $1 >/dev/null 2>&1; then | |
| echo "1" | |
| else | |
| echo "0" | |
| fi | |
| } | |
| if [ "$#" -ne 2 ]; then | |
| if [ "$1" != "list" ] && [ "$1" != "status" ]; then | |
| echo -e $TOMCAT_USAGE | |
| exit 1 | |
| else | |
| STATUS_TARGET="all" | |
| fi | |
| else | |
| STATUS_TARGET=$TOMCAT_NAME | |
| fi | |
| case $1 in | |
| start) | |
| start | |
| ;; | |
| stop) | |
| stop | |
| ;; | |
| restart) | |
| stop | |
| start | |
| ;; | |
| status) | |
| status $STATUS_TARGET | |
| ;; | |
| *) | |
| 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