Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ruseel/e3eb197335597e7527b4 to your computer and use it in GitHub Desktop.
Save ruseel/e3eb197335597e7527b4 to your computer and use it in GitHub Desktop.

Revisions

  1. Timothy Hutz revised this gist May 27, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Apache Tomcat 8 Start stop script init.d script
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!/bin/bash
    # chkconfig: 2345 95 20
    # description: This application was developed by me and is tested on this server
    # processname: my_app
    #
    # Tomcat 8 start/stop/status init.d script
    # Initially forked from: https://gist.github.com/valotas/1000094
    @@ -127,4 +130,4 @@ case $1 in
    echo -e $TOMCAT_USAGE
    ;;
    esac
    exit 0
    exit 0
  2. @miglen miglen renamed this gist Sep 4, 2014. 1 changed file with 36 additions and 34 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    #!/bin/bash
    #
    # Tomcat 7 start/stop/status script
    # Forked from: https://gist.github.com/valotas/1000094
    # Tomcat 8 start/stop/status init.d script
    # Initially forked from: https://gist.github.com/valotas/1000094
    # @author: Miglen Evlogiev <[email protected]>
    #
    # Release updates:
    # 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
    #
    # Added termination proccess

    #Location of JAVA_HOME (bin files)
    export JAVA_HOME=/usr/java/jdk1.7.0_21
    export JAVA_HOME=/usr/java/latest

    #Add Java binary files to PATH
    export PATH=$JAVA_HOME/bin:$PATH
    @@ -21,13 +21,13 @@ export PATH=$JAVA_HOME/bin:$PATH
    export CATALINA_HOME=/usr/share/tomcat

    #CATALINA_BASE is the location of the configuration files of this instance of Tomcat
    export CATALINA_BASE=/usr/share/tomcat5
    export CATALINA_BASE=/var/apphome/tomcat-node-1

    #TOMCAT_USER is the default user of tomcat
    export TOMCAT_USER=tomcat

    #TOMCAT_USAGE is the message if this script is called without any options
    TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
    TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

    #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
    SHUTDOWN_WAIT=20
    @@ -46,10 +46,10 @@ start() {
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh tomcat
    #/bin/su -p -s /bin/sh $TOMCAT_USER
    if [ `user_exists $TOMCAT_USER` = "1" ]
    then
    su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
    /bin/su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
    else
    sh $CATALINA_HOME/bin/startup.sh
    fi
    @@ -64,13 +64,18 @@ status(){
    else echo -e "\e[00;31mTomcat is not running\e[00m"
    fi
    }


    terminate() {
    echo -e "\e[00;31mTerminating Tomcat\e[00m"
    kill -9 $(tomcat_pid)
    }

    stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh tomcat
    #/bin/su -p -s /bin/sh $TOMCAT_USER
    sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    @@ -83,8 +88,8 @@ stop() {
    done

    if [ $count -gt $kwait ]; then
    echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
    kill -9 $pid
    echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
    terminate
    fi
    else
    echo -e "\e[00;31mTomcat is not running\e[00m"
    @@ -102,27 +107,24 @@ user_exists(){
    }

    case $1 in

    start)
    start
    ;;

    stop)
    stop
    ;;

    restart)
    stop
    start
    ;;

    status)
    status

    ;;

    *)
    echo -e $TOMCAT_USAGE
    ;;
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    start
    ;;
    status)
    status
    ;;
    kill)
    terminate
    ;;
    *)
    echo -e $TOMCAT_USAGE
    ;;
    esac
    exit 0
  3. @miglen miglen renamed this gist May 16, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @miglen miglen revised this gist May 16, 2013. 1 changed file with 90 additions and 64 deletions.
    154 changes: 90 additions & 64 deletions tomcat.sh
    Original file line number Diff line number Diff line change
    @@ -1,102 +1,128 @@
    #!/bin/bash
    #
    # tomcat7 This shell script takes care of starting and stopping Tomcat
    # Tomcat 7 start/stop/status script
    # Forked from: https://gist.github.com/valotas/1000094
    # @author: Miglen Evlogiev <[email protected]>
    #
    # chkconfig: - 80 20
    # Release updates:
    # 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
    #
    ### BEGIN INIT INFO
    # Provides: tomcat7
    # Required-Start: $network $syslog
    # Required-Stop: $network $syslog
    # Default-Start:
    # Default-Stop:
    # Description: Release implementation for Servlet 2.5 and JSP 2.1
    # Short-Description: start and stop tomcat
    ### END INIT INFO

    ## Source function library.
    #. /etc/rc.d/init.d/functions
    export JAVA_HOME=/usr/java/default
    export JAVA_OPTS="-Dfile.encoding=UTF-8 \
    -Dcatalina.logbase=/var/log/tomcat7 \
    -Dnet.sf.ehcache.skipUpdateCheck=true \
    -XX:+DoEscapeAnalysis \
    -XX:+UseConcMarkSweepGC \
    -XX:+CMSClassUnloadingEnabled \
    -XX:+UseParNewGC \
    -XX:MaxPermSize=128m \
    -Xms512m -Xmx512m"

    #Location of JAVA_HOME (bin files)
    export JAVA_HOME=/usr/java/jdk1.7.0_21

    #Add Java binary files to PATH
    export PATH=$JAVA_HOME/bin:$PATH
    TOMCAT_HOME=/usr/share/tomcat7

    #CATALINA_HOME is the location of the bin files of Tomcat
    export CATALINA_HOME=/usr/share/tomcat

    #CATALINA_BASE is the location of the configuration files of this instance of Tomcat
    export CATALINA_BASE=/usr/share/tomcat5

    #TOMCAT_USER is the default user of tomcat
    export TOMCAT_USER=tomcat

    #TOMCAT_USAGE is the message if this script is called without any options
    TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

    #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
    SHUTDOWN_WAIT=20

    tomcat_pid() {
    echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
    echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
    }

    start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    if [ -n "$pid" ]
    then
    echo "Tomcat is already running (pid: $pid)"
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
    else
    # Start tomcat
    echo "Starting tomcat"
    ulimit -n 100000
    umask 007
    /bin/su -p -s /bin/sh tomcat $TOMCAT_HOME/bin/startup.sh
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh tomcat
    if [ `user_exists $TOMCAT_USER` = "1" ]
    then
    su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
    else
    sh $CATALINA_HOME/bin/startup.sh
    fi
    status
    fi


    return 0
    }


    status(){
    pid=$(tomcat_pid)
    if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
    else echo -e "\e[00;31mTomcat is not running\e[00m"
    fi
    }

    stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
    echo "Stoping Tomcat"
    /bin/su -p -s /bin/sh tomcat $TOMCAT_HOME/bin/shutdown.sh

    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh tomcat
    sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
    echo -n -e "\nwaiting for processes to exit";
    echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
    sleep 1
    let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
    echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
    echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
    kill -9 $pid
    fi
    else
    echo "Tomcat is not running"
    echo -e "\e[00;31mTomcat is not running\e[00m"
    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)
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
    echo "Tomcat is running with pid: $pid"
    else
    echo "Tomcat is not running"
    fi
    ;;

    start)
    start
    ;;

    stop)
    stop
    ;;

    restart)
    stop
    start
    ;;

    status)
    status

    ;;

    *)
    echo -e $TOMCAT_USAGE
    ;;
    esac
    exit 0
  5. @valotas valotas created this gist May 31, 2011.
    102 changes: 102 additions & 0 deletions tomcat.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    #!/bin/bash
    #
    # tomcat7 This shell script takes care of starting and stopping Tomcat
    #
    # chkconfig: - 80 20
    #
    ### BEGIN INIT INFO
    # Provides: tomcat7
    # Required-Start: $network $syslog
    # Required-Stop: $network $syslog
    # Default-Start:
    # Default-Stop:
    # Description: Release implementation for Servlet 2.5 and JSP 2.1
    # Short-Description: start and stop tomcat
    ### END INIT INFO

    ## Source function library.
    #. /etc/rc.d/init.d/functions
    export JAVA_HOME=/usr/java/default
    export JAVA_OPTS="-Dfile.encoding=UTF-8 \
    -Dcatalina.logbase=/var/log/tomcat7 \
    -Dnet.sf.ehcache.skipUpdateCheck=true \
    -XX:+DoEscapeAnalysis \
    -XX:+UseConcMarkSweepGC \
    -XX:+CMSClassUnloadingEnabled \
    -XX:+UseParNewGC \
    -XX:MaxPermSize=128m \
    -Xms512m -Xmx512m"
    export PATH=$JAVA_HOME/bin:$PATH
    TOMCAT_HOME=/usr/share/tomcat7
    SHUTDOWN_WAIT=20

    tomcat_pid() {
    echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
    }

    start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
    echo "Tomcat is already running (pid: $pid)"
    else
    # Start tomcat
    echo "Starting tomcat"
    ulimit -n 100000
    umask 007
    /bin/su -p -s /bin/sh tomcat $TOMCAT_HOME/bin/startup.sh
    fi


    return 0
    }

    stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
    echo "Stoping Tomcat"
    /bin/su -p -s /bin/sh tomcat $TOMCAT_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
    echo -n -e "\nwaiting for processes to exit";
    sleep 1
    let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
    echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
    kill -9 $pid
    fi
    else
    echo "Tomcat is not running"
    fi

    return 0
    }

    case $1 in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    start
    ;;
    status)
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
    echo "Tomcat is running with pid: $pid"
    else
    echo "Tomcat is not running"
    fi
    ;;
    esac
    exit 0