Skip to content

Instantly share code, notes, and snippets.

@reflexdemon
Last active September 27, 2016 21:20
Show Gist options
  • Save reflexdemon/bf2a395e6472e97aa765 to your computer and use it in GitHub Desktop.
Save reflexdemon/bf2a395e6472e97aa765 to your computer and use it in GitHub Desktop.
Handling Tomcat domains

Running Tomcat on separate domain folders

tomcat_common.sh

#!/bin/bash

#Tomcat's catalina.sh require this line
export JAVA_OPTS=$JAVA_OPTIONS

if [[ -z "$JAVA_HOME" ]]; then
    echo "JAVA_HOME is not set please ensure to set this variable before calling your command"
    exit 1
fi

if [[ -z "$TOMCAT_HOME" ]]; then
    echo "TOMCAT_HOME is not set please ensure to set this variable before calling your command"
    exit 1
fi

if [[ -z "$CATALINA_BASE" ]]; then
    echo "CATALINA_BASE is not set please ensure to set this variable before calling your command"
    exit 1
fi

if [[ -z "$APP_KEY" ]]; then
    echo "APP_KEY is not set please ensure to set this variable before calling your command"
    exit 1
fi

if [[ -z "$JAVA_OPTS" ]]; then
    echo "JAVA_OPTS is not set please ensure to set this variable before calling your command"
    exit 1
fi

prompt_confirm() {
  while true; do
    read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
    case $REPLY in
      [yY]) echo ; return 1 ;;
      [nN]) echo ; return 0 ;;
      *) printf " \033[31m %s \n\033[0m" "invalid input"
    esac 
  done  
}

###########################################################################
# Purpose : Function to execute the command passed using command line
# Argument: Command to be executed
###########################################################################
function execute_command {
    case "$1" in
        (start)
            echo "Starting Tomcat using $TOMCAT_HOME/bin/startup.sh"
            $TOMCAT_HOME/bin/startup.sh
        ;;
        ###########################################################################
        (restart)
            echo "Killing Current Running Process (if any)."
            ps -eaf | grep -v grep | grep $APP_KEY | awk '{print $2}' | xargs kill
            echo "Starting Tomcat using $TOMCAT_HOME/bin/startup.sh"
            $TOMCAT_HOME/bin/startup.sh
        ;;
        ###########################################################################
        (stop)
            echo "Stoping Tomcat using $TOMCAT_HOME/bin/shutdown.sh"
            $TOMCAT_HOME/bin/shutdown.sh
        ;;
        ###########################################################################
        (kill)
            # echo "Killing Tomcat using ps -ef | grep $APP_KEY | grep -v grep | awk '{print $2}' | xargs kill"
            prompt_confirm "Did you try stop?" || ps -eaf | grep -v grep | grep $APP_KEY | awk '{print $2}' | xargs kill
        ;;
        ###########################################################################
        (kill9)
            # echo "Killing Tomcat using ps -ef | grep $APP_KEY | grep -v grep | awk '{print $2}' | xargs kill -9"
            prompt_confirm "Did you try stop?" || ps -eaf | grep -v grep | grep $APP_KEY | awk '{print $2}' | xargs kill -9
        ;;
        ###########################################################################
        (tail)
            echo "$CATALINA_BASE/logs/catalina.out"
            tail -f $CATALINA_BASE/logs/catalina.out
        ;;
        ###########################################################################
        (tail_all)
            echo "$CATALINA_BASE/logs/*"
            tail -f $CATALINA_BASE/logs/*
        ;;
        ###########################################################################
        (home_tail)
            echo "$CATALINA_HOME/logs/catalina.out"
            tail -f $CATALINA_HOME/logs/catalina.out
        ;;
        ###########################################################################
        (home_tail_all)
            echo "$CATALINA_HOME/logs/*"
            tail -f $CATALINA_HOME/logs/*
        ;;
        ###########################################################################
        (home_clean)
            echo "Delete all file inside $CATALINA_HOME/logs/*"
            prompt_confirm "Delete Logs?" || rm -rf $CATALINA_HOME/logs
            if [[ ! -d "$CATALINA_HOME/logs" ]]; then
                mkdir $CATALINA_HOME/logs
            fi
        ;;
        ###########################################################################
        (clean)
            echo "Delete all file inside $CATALINA_BASE/logs/*"
            prompt_confirm "Delete Logs?" || rm -rf $CATALINA_BASE/logs
            if [[ ! -d "$CATALINA_BASE/logs" ]]; then
                mkdir $CATALINA_BASE/logs
            fi
        ;;
        ###########################################################################
        (status)
            #echo "Running Tomcat process..."
            OUTPUT="$(ps -eaf | grep -v grep | grep $APP_KEY)"
            COUNT="$(echo \"${OUTPUT}\" | wc -w)"

            # echo "${OUTPUT}"
            # echo "COUNT === ${COUNT}"

            if [ "$COUNT" -gt "1" ]; then
                echo "${OUTPUT}"
            else
                echo "${APP_KEY} is not running..."
            fi
        ;;
        ###########################################################################
        (*)
            # echo "Running Tomcat process..."
            # ps -eaf | grep -v grep | grep $APP_KEY
            echo "Usage: $0 [start|stop|status|kill|kill9|restart|tail|tail_all|clean|home_clean]"
       exit 1
    esac
} 

# echo "JAVA_OPTS = $JAVA_OPTS"
execute_command $1

#If user wants to tail post command execution we pass the second argument for parsing.
if [[ $2 == *"tail_all"* ]]
then
  execute_command $2
fi

#If user wants to tail post command execution we pass the second argument for parsing.
if [[ $2 == *"tail"* ]]
then
  execute_command $2
fi

domain.sh

#!/bin/bash

JAVA_HOME=/usr/local/jdk1.8


TOMCAT_HOME=/usr/local/apache-tomcat-8.0.28



export CATALINA_BASE=/usr/local/tomcat_domains/domain

# Identify this process for monitoring.
APP_KEY="tomcat-domain-`hostname -s`"


JAVA_OPTIONS="-Djava.endorsed.dirs=$CATALINA_BASE/endorsed \
-Dcatalina.base=$CATALINA_BASE \
-Dcatalina.home=$TOMCAT_HOME \
-Djava.io.tmpdir=$CATALINA_BASE/tmp \
-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Dapp.key=$APP_KEY"

#Tomcat's catalina.sh require this line
export JAVA_OPTS=$JAVA_OPTIONS


#Call the common Stuff
CURRENTDIR=`dirname $0`
. $CURRENTDIR/../tomcat_common.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment