不调用shutdown.sh,不直接kill 进程,tomcat启动、关闭脚本

这篇文章,是上一篇 Jenkins 部署 远程调试的继续,

为了解决的问题

为了解决这样的一个问题:假设在用户 abc 的home 目录 /home/abc 下有两个tomcat,分别放在了
apache-tomcat-8.5.29_projectsapache-tomcat-8.5.29_Jenkins 目录下。

一个监听7070端口,一个监听8080端口。

调用 /home/abc/apache-tomcat-8.5.29_projects/bin/shutdown.sh 会把 在 apache-tomcat-8.5.29_Jenkins 目录启动的tomcat也杀掉

虽然有简单的脚本 start.shstop.sh 可以解决这个问题。但是这个两个脚本有个小问题:CATALINA_HOME 和 CATALINA_BASE 路径是写死的。这样的话,如果在多个地方部署的话,需要修改多次。

假如有一个脚本,跟start.sh 和stop.sh 一样,放在了 apache-tomcat-8.5.29/bin 目录下,但是不用写死路径,而是以当前脚本的路径为启动,自己去判断 CATALINA_HOME 和 CATALINA_BASE 就好了。

start.sh

export JAVA_HOME=/usr/java/jdk1.8.0_152
export CATALINA_HOME=/home/abc/apache-tomcat-8.5.29_projects
export CATALINA_BASE=/home/abc/apache-tomcat-8.5.29_projects
export CATALINA_PID=${CATALINA_BASE}/tomcat.pid

${CATALINA_HOME}/bin/catalina.sh start

stop.sh

export JAVA_HOME=/usr/java/jdk1.8.0_152
export CATALINA_HOME=/home/abc/apache-tomcat-8.5.29_projects
export CATALINA_BASE=/home/abc/apache-tomcat-8.5.29_projects
export CATALINA_PID=${CATALINA_BASE}/tomcat.pid

${CATALINA_HOME}/bin/catalina.sh stop

脚本

这个脚本是根据ubuntu自带的 /etc/init.d/tomcat8 修改而来。

#!/bin/sh
#
# this file is based on ubuntu's /etc/init.d/tomcat8
#
# /etc/init.d/tomcat8 -- startup script for the Tomcat 8 servlet engine
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.
# Modified for Tomcat6 by Thierry Carrez <thierry.carrez@ubuntu.com>.
# Modified for Tomcat7 by Ernesto Hernandez-Novich <emhn@itverx.com.ve>.
# Additional improvements by Jason Brittain <jason.brittain@mulesoft.com>.
#
### BEGIN INIT INFO
# Provides:          tomcat8
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Tomcat.
# Description:       Start the Tomcat servlet engine.
### END INIT INFO


# use tomcat_dir/bin as start directory
# CATALINA_HOME=/usr/share/$NAME
CWD=$(realpath $0)

CWD=$(dirname $CWD)

CWD=$(dirname $CWD)


# Directory for per-instance configuration files and webapps
CATALINA_HOME=$CWD
CATALINA_BASE=$CWD
WORKING_DIR=$(basename $CWD)


set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=tomcat
DEFAULT=./config.sh
JVM_TMP=/tmp/tomcat-$WORKING_DIR

DESC="Tomcat servlet engine in ${CATALINA_BASE}"

# don't need root privilege
# if [ `id -u` -ne 0 ]; then
#     echo "You need root privileges to run this script"
#     exit 1
# fi

# Make sure tomcat is started with system locale
if [ -e /etc/default/locale -a -r /etc/default/locale ]; then
    . /etc/default/locale
    export LANG
elif [ -e /etc/locale.conf -a -r /etc/locale.conf ]; then
    . /etc/locale.conf
    export LANG
fi

# The following variables can be overwritten in $DEFAULT

# Run Tomcat 8 as this user ID and group ID
# TOMCAT8_USER=tomcat8
# TOMCAT8_GROUP=tomcat8
TOMCAT8_USER=$(whoami)
TOMCAT8_GROUP=$(whoami)

# this is a work-around until there is a suitable runtime replacement
# for dpkg-architecture for arch:all packages
# this function sets the variable JDK_DIRS

# let the user set JAVA_HOME variable, other than guess the JDK dir
if [ -z "${JAVA_HOME-}" ]; then
    echo "JAVA_HOME is not defined. exiting....";
    exit 1;
else

    if [ ! -d $JAVA_HOME ]; then
        echo "$JAVA_HOME is not a directory. existing....";
        exit 1;
    else
        if [ ! -f $JAVA_HOME/bin/java ]; then
            echo "${JAVA_HOME}/bin/java does not exists. exiting....";
            exit 1;
        fi
    fi
fi

# Use the Java security manager? (yes/no)
TOMCAT8_SECURITY=no

# Default Java options
# Set java.awt.headless=true if JAVA_OPTS is not set so the
# Xalan XSL transformer can work without X11 display on JDK 1.4+
# It also looks like the default heap size of 64M is not enough for most cases
# so the maximum heap size is set to 128M
if [ -z "$JAVA_OPTS" ]; then
    JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
fi

# End of variables that can be overwritten in $DEFAULT


if [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; then
    echo "$NAME is not installed"
    exit 1
fi


#if [ -z "$CATALINA_TMPDIR" ]; then
    CATALINA_TMPDIR="$JVM_TMP"
#fi

# Set the JSP compiler if set in the tomcat8.default file
if [ -n "$JSP_COMPILER" ]; then
    JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\""
fi

SECURITY=""
if [ "$TOMCAT8_SECURITY" = "yes" ]; then
    SECURITY="-security"
fi

# the main purpose of this script is to set the CATALINA_PID 
# Define other required variables
# CATALINA_PID="/var/run/$NAME.pid"
CATALINA_PID="$CATALINA_BASE/conf/${NAME}_pid.pid"
CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"

# Look for Java Secure Sockets Extension (JSSE) JARs
if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
    JSSE_HOME="${JAVA_HOME}/jre/"
fi

catalina_sh() {
    # Escape any double quotes in the value of JAVA_OPTS
    JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')"

    AUTHBIND_COMMAND=""
    if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
        AUTHBIND_COMMAND="/usr/bin/authbind --deep /bin/bash -c "
    fi

    # Define the command to run Tomcat's catalina.sh as a daemon
    # set -a tells sh to export assigned variables to spawned shells.
    TOMCAT_SH="set -a; JAVA_HOME=\"$JAVA_HOME\"; \
        CATALINA_HOME=\"$CATALINA_HOME\"; \
        CATALINA_BASE=\"$CATALINA_BASE\"; \
        JAVA_OPTS=\"$JAVA_OPTS\"; \
        CATALINA_PID=\"$CATALINA_PID\"; \
        CATALINA_TMPDIR=\"$CATALINA_TMPDIR\"; \
        LANG=\"$LANG\"; JSSE_HOME=\"$JSSE_HOME\"; \
        cd \"$CATALINA_BASE\"; \
        \"$CATALINA_SH\" $@"


    if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
        TOMCAT_SH="'$TOMCAT_SH'"
    fi

    # Run the catalina.sh script as a daemon
    set +e
    if [ ! -f "$CATALINA_BASE"/logs/catalina.out ]; then
        install -o $TOMCAT8_USER -g adm -m 644 /dev/null "$CATALINA_BASE"/logs/catalina.out
    fi
    #install -o $TOMCAT8_USER -g adm -m 644 /dev/null "$CATALINA_PID"

    cd $CATALINA_BASE 
    JAVA_HOME=$JAVA_HOME CATALINA_HOME=$CATALINA_HOME CATALINA_BASE=$CATALINA_BASE JAVA_OPTS="$JAVA_OPTS" CATALINA_PID=$CATALINA_PID CATALINA_TMPDIR=$CATALINA_TMPDIR LANG=$LANG JSSE_HOME=$JSSE_HOME $CATALINA_SH $@


    status="$?"
    set +a -e
    return $status
}

case "$1" in
  start)
    if [ -z "$JAVA_HOME" ]; then
        echo "no JDK or JRE found - please set JAVA_HOME"
        exit 1
    fi

    if [ ! -d "$CATALINA_BASE/conf" ]; then
        echo "invalid CATALINA_BASE: $CATALINA_BASE"
        exit 1
    fi

    echo "Starting $DESC" "$NAME"
    if [ ! -f $CATALINA_PID ]; then
        echo "$CATALINA_PID does not exists"
    else
        xpid=$(cat $CATALINA_PID)
        ypid=$(ps ax | grep tomcat | grep $CATALINA_BASE | cut -d " " -f 2)
    if [ ! -z $yid ]; then
        if [ $xpid = $ypid ]; then
            echo "tomcat of $CATALINA_BASE already runnning. existing...";
            exit 0;
        fi
    fi
    fi

        # Regenerate POLICY_CACHE file
        umask 022
        # Remove / recreate JVM_TMP directory
        rm -rf "$JVM_TMP"
        mkdir "$JVM_TMP" || {
            echo "could not create JVM temporary directory"
            exit 1
        }
        # chown -h $TOMCAT8_USER "$JVM_TMP"

        catalina_sh start $SECURITY
        sleep 5

        if [ -r $CATALINA_PID ]; then
            xpid=$(cat $CATALINA_PID)

            # pid is not written into CATALINA_PID file
            if [ -z $xpid ]; then
                echo "pid is not written into CATALINA_PID file. exiting...."
                exit 1;
            fi

            ypid=$(ps ax | grep tomcat | grep $CATALINA_BASE | cut -d " " -f 2)
            if [ ! -z $pid ]; then
                echo "ypid is not null"

                if [ $xpid != $ypid ]; then
                    echo "tomcat of $CATALINA_BASE something went wrong, pid from ps and pid from $CATALINA_PID are different.";
                    exit 1;
                fi
            fi
        fi
    ;;
  stop)
    echo "Stopping $DESC" "$NAME"

    set +e
    if [ -f "$CATALINA_PID" ]; then 

        catalina_sh stop -force
        if [ $? -eq 1 ]; then
            echo "$DESC is not running but pid file exists, cleaning up"
        elif [ $? -eq 3 ]; then
            PID="`cat $CATALINA_PID`"
            echo "Failed to stop $NAME  pid ${PID} "
            exit 1
        fi
        rm -f "$CATALINA_PID"
        rm -rf "$JVM_TMP"
    else
        echo "tomcat in ${CATALINA_BASE} not running"
    fi
    set -e
    ;;
   status)
    set +e

    if [ -r $CATALINA_PID ]; then
        xpid=$(cat $CATALINA_PID)

        # pid is not written into CATALINA_PID file
        if [ -z $xpid ]; then
            echo "tomcat of $CATALINA_BASE pid file exists, but empty. exiting...."
            exit 1;
        fi

        ypid=$(ps ax | grep tomcat | grep $CATALINA_BASE | cut -d " " -f 2)
    if [ ! -z $pid ]; then
        if [ $xpid != $ypid ]; then
            echo "tomcat of $CATALINA_BASE something went wrong, pid from ps and pid from $CATALINA_PID are different.";
            exit 1;
        fi

    else
            echo "tomcat of $CATALINA_BASE is running with pid `cat $CATALINA_PID`"
    fi

    else
        echo "tomcat of $CATALINA_BASE not runing...."
        exit 3
    fi

    set -e
        ;;
  restart|force-reload)
    if [ -f "$CATALINA_PID" ]; then
        $0 stop -force
        sleep 1
    fi
    $0 start
    ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac

exit 0

使用效果

aa@ubuntu:~/projects/bin$ ./xontrol.sh status
tomcat of /home/aa/projects is running with pid 14111


aa@ubuntu:~/projects/bin$ ./xontrol.sh stop
Stopping Tomcat servlet engine in /home/aa/projects tomcat
Using CATALINA_BASE:   /home/aa/projects
Using CATALINA_HOME:   /home/aa/projects
Using CATALINA_TMPDIR: /tmp/tomcat-projects
Using JRE_HOME:        /usr/lib/jvm/java-8-openjdk-amd64
Using CLASSPATH:       /home/aa/projects/bin/bootstrap.jar:/home/aa/projects/bin/tomcat-juli.jar
Using CATALINA_PID:    /home/aa/projects/conf/tomcat_pid.pid
PID file is empty and has been ignored.
Listening for transport dt_socket at address: 5005
Tomcat stopped.


aa@ubuntu:~/projects/bin$ ./xontrol.sh status
tomcat of /home/aa/projects not runing....


aa@ubuntu:~/projects/bin$ ./xontrol.sh start
Starting Tomcat servlet engine in /home/aa/projects tomcat
/home/aa/projects/conf/tomcat_pid.pid does not exists
Using CATALINA_BASE:   /home/aa/projects
Using CATALINA_HOME:   /home/aa/projects
Using CATALINA_TMPDIR: /tmp/tomcat-projects
Using JRE_HOME:        /usr/lib/jvm/java-8-openjdk-amd64
Using CLASSPATH:       /home/aa/projects/bin/bootstrap.jar:/home/aa/projects/bin/tomcat-juli.jar
Using CATALINA_PID:    /home/aa/projects/conf/tomcat_pid.pid
Tomcat started.


aa@ubuntu:~/projects/bin$ ./xontrol.sh status
tomcat of /home/aa/projects is running with pid 94702
aa@ubuntu:~/projects/bin$

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容