分析catalina.sh腳本的目的,一個(gè)是學(xué)習(xí)腳本中shell的各類用法,還有就是為編寫tomcat多服務(wù)器遠(yuǎn)程啟動(dòng)腳本做準(zhǔn)備,實(shí)現(xiàn)版本上線自動(dòng)化無(wú)人干預(yù)部署,此前作者已發(fā)表“ tomcat變量環(huán)境腳本setclasspath.sh分析”來(lái)為此篇作為鋪墊,需要兩篇文章一起看,才能全面的分析出tomcat的啟動(dòng)過程。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、順慶網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為順慶等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
tomcat版本:6.0.35
#!/bin/sh # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ----------------------------------------------------------------------------- # Start/Stop Script for the CATALINA Server # # Environment Variable Prerequisites # # CATALINA_HOME May point at your Catalina "build" directory. # # CATALINA_BASE (Optional) Base directory for resolving dynamic portions # of a Catalina installation. If not present, resolves to # the same directory that CATALINA_HOME points to. # # CATALINA_OUT (Optional) Full path to a file where stdout and stderr # will be redirected. # Default is $CATALINA_BASE/logs/catalina.out # # CATALINA_OPTS (Optional) Java runtime options used when the "start", # or "run" command is executed. # # CATALINA_TMPDIR (Optional) Directory path location of temporary directory # the JVM should use (java.io.tmpdir). Defaults to # $CATALINA_BASE/temp. # # JAVA_HOME Must point at your Java Development Kit installation. # Required to run the with the "debug" argument. # # JRE_HOME Must point at your Java Development Kit installation. # Defaults to JAVA_HOME if empty. # # JAVA_OPTS (Optional) Java runtime options used when the "start", # "stop", or "run" command is executed. # # JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories # containing some jars in order to allow replacement of APIs # created outside of the JCP (i.e. DOM and SAX from W3C). # It can also be used to update the XML parser implementation. # Defaults to $CATALINA_HOME/endorsed. # # JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start" # command is executed. The default is "dt_socket". # # JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start" # command is executed. The default is 8000. # # JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start" # command is executed. Specifies whether JVM should suspend # execution immediately after startup. Default is "n". # # JPDA_OPTS (Optional) Java runtime options used when the "jpda start" # command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS, # and JPDA_SUSPEND are ignored. Thus, all required jpda # options MUST be specified. The default is: # # -agentlib:jdwp=transport=$JPDA_TRANSPORT, # address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND # # CATALINA_PID (Optional) Path of the file which should contains the pid # of catalina startup java process, when start (fork) is used # # LOGGING_CONFIG (Optional) Override Tomcat's logging config file # Example (all one line) # LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties" # # LOGGING_MANAGER (Optional) Override Tomcat's logging manager # Example (all one line) # LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" # # $Id: catalina.sh 1146097 2011-07-13 15:25:05Z markt $ # ----------------------------------------------------------------------------- export JRE_HOME=/usr/java/jdk1.6.0_38 export CATALINA_HOME=/home/xrltest1/tomcat JAVA_OPTS="-server -Xms2048m -Xmx2048m -Xmn768m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:+UseParallelOldGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/xrltest1/tomcat/dumpfile/heap.bin -Xloggc:/home/xrltest1/tomcat/logs/gc.log" #作者添加的環(huán)境申明,如果一臺(tái)服務(wù)器中有多臺(tái)tomcat要使用不同版本的JVM,就可以直接這這邊添加JRE_HOME,不需要再/etc/profile.d中再配置JRE_HOME環(huán)境變量 # OS specific support. $var _must_ be set to either true or false. #此處語(yǔ)句判斷操作系統(tǒng),同時(shí)對(duì)操作系統(tǒng)支持 #os400是 IBM的AIX #darwin是MacOSX 操作環(huán)境的操作系統(tǒng)成份 #Darwin是windows平臺(tái)上運(yùn)行的類UNIX模擬環(huán)境 cygwin=false os400=false darwin=false case "`uname`" in CYGWIN*) cygwin=true;; OS400*) os400=true;; Darwin*) darwin=true;; esac # resolve links - $0 may be a softlink #此處的RPG抓取的是文件名,因?yàn)榭赡苁欠?hào)鏈接,所以下面循環(huán)語(yǔ)句的作用就是找到文件真實(shí)源路徑 PRG="$0" while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done #獲取腳本目錄真實(shí)目錄地址 # Get standard environment variables PRGDIR=`dirname "$PRG"` # Only set CATALINA_HOME if not already set [ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd` # Copy CATALINA_BASE from CATALINA_HOME if not already set [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME" #上面兩個(gè)語(yǔ)句判斷變量$CATALINA_HOME和$CATALINA_BASE是否存在,不存在則給予附值 #CATALINA_BASE="$CATALINA_HOME" # Ensure that any user defined CLASSPATH variables are not used on startup, # but allow them to be specified in setenv.sh, in rare case when it is needed. CLASSPATH= #test –r File 文件存在并且可讀 if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then . "$CATALINA_BASE/bin/setenv.sh" elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then . "$CATALINA_HOME/bin/setenv.sh" fi #默認(rèn)的tomcat的bin目錄下沒有這個(gè)文件,可以自行編寫setenv.sh這個(gè)文件設(shè)定環(huán)境變量 # For Cygwin, ensure paths are in UNIX format before anything is touched if $cygwin; then [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` [ -n "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"` [ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"` [ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"` [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"` fi # For OS400 if $os400; then # Set job priority to standard for interactive (interactive - 6) by using # the interactive priority - 6, the helper threads that respond to requests # will be running at the same priority as interactive jobs. COMMAND='chgjob job('$JOBNAME') runpty(6)' system $COMMAND # Enable multi threading export QIBM_MULTI_THREADED=Y fi # Get standard Java environment variables if $os400; then # -r will Only work on the os400 if the files are: # 1. owned by the user # 2. owned by the PRIMARY group of the user # this will not work if the user belongs in secondary groups BASEDIR="$CATALINA_HOME" . "$CATALINA_HOME"/bin/setclasspath.sh else #這點(diǎn)一定要注意了,本腳本中沒有賦值卻突然冒出的變量 #都是在setclasspath.sh這個(gè)腳本中執(zhí)行賦值的。比如$_RUNJAVA #作者會(huì)在下一篇文章中,分析setclasspath.sh這個(gè)腳本 if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then BASEDIR="$CATALINA_HOME" . "$CATALINA_HOME"/bin/setclasspath.sh else echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh" echo "This file is needed to run this program" exit 1 fi fi #以上是出現(xiàn)在其他操作環(huán)境下的腳本的可用性設(shè)置,因默認(rèn)linux系統(tǒng),上面就不做解讀了 if [ -z "$CATALINA_BASE" ] ; then CATALINA_BASE="$CATALINA_HOME" fi #將變量賦值 # Add tomcat-juli.jar and bootstrap.jar to classpath # tomcat-juli.jar can be over-ridden per instance if [ ! -z "$CLASSPATH" ] ; then CLASSPATH="$CLASSPATH": fi #雖然前面已將做出CATALINA_BASE="$CATALINA_HOME"的語(yǔ)句 #此處還是添加了判斷,增強(qiáng)了代碼的健壯性 #添加變量$CLASSPATH的值 if [ "$CATALINA_BASE" != "$CATALINA_HOME" ] && [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then CLASSPATH="$CLASSPATH""$CATALINA_BASE"/bin/tomcat-juli.jar:"$CATALINA_HOME"/bin/bootstrap.jar else CLASSPATH="$CLASSPATH""$CATALINA_HOME"/bin/bootstrap.jar fi #確認(rèn)日志路徑$CATALINA_OUT #這部分主要判斷這些變量是否預(yù)定義,有就以預(yù)定義為主,沒有就設(shè)置為默認(rèn)格式 if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out fi if [ -z "$CATALINA_TMPDIR" ] ; then # Define the java.io.tmpdir to use for Catalina CATALINA_TMPDIR="$CATALINA_BASE"/temp fi # Bugzilla 37848: When no TTY is available, don't output to console #獲取當(dāng)前shell運(yùn)行的終端設(shè)備 have_tty=0 if [ "`tty`" != "not a tty" ]; then have_tty=1 fi # For Cygwin, switch paths to Windows format before running java if $cygwin; then JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"` JRE_HOME=`cygpath --absolute --windows "$JRE_HOME"` CATALINA_HOME=`cygpath --absolute --windows "$CATALINA_HOME"` CATALINA_BASE=`cygpath --absolute --windows "$CATALINA_BASE"` CATALINA_TMPDIR=`cygpath --absolute --windows "$CATALINA_TMPDIR"` CLASSPATH=`cygpath --path --windows "$CLASSPATH"` JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"` fi # Set juli LogManager config file if it is present and an override has not been issued #確認(rèn)$LOGGING_CONFIG變量 if [ -z "$LOGGING_CONFIG" ]; then if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties" else # Bugzilla 45585 LOGGING_CONFIG="-Dnop" fi fi #確認(rèn)$LOGGING_MANAGER變量 if [ -z "$LOGGING_MANAGER" ]; then JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" else JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER" fi # ----- Execute The Requested Command ----------------------------------------- # Bugzilla 37848: only output this if we have a TTY #正常情況下have_tty=1 if [ $have_tty -eq 1 ]; then echo "Using CATALINA_BASE: $CATALINA_BASE" echo "Using CATALINA_HOME: $CATALINA_HOME" echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR" if [ "$1" = "debug" ] ; then echo "Using JAVA_HOME: $JAVA_HOME" else echo "Using JRE_HOME: $JRE_HOME" fi echo "Using CLASSPATH: $CLASSPATH" #以下這句判斷設(shè)置的$CATALINA_PID變量如果不存在,則顯示"Using CATALINA_PID: $CATALINA_PID",如果存在則不顯示 if [ ! -z "$CATALINA_PID" ]; then echo "Using CATALINA_PID: $CATALINA_PID" fi fi #以上就是在啟動(dòng)tomcat是輸出的環(huán)境變量信息 # #獲取第一次參數(shù),jpda在后面的說明為:jpda start Start Catalina under JPDA debugger if [ "$1" = "jpda" ] ; then if [ -z "$JPDA_TRANSPORT" ]; then JPDA_TRANSPORT="dt_socket" fi if [ -z "$JPDA_ADDRESS" ]; then JPDA_ADDRESS="8000" fi if [ -z "$JPDA_SUSPEND" ]; then JPDA_SUSPEND="n" fi if [ -z "$JPDA_OPTS" ]; then JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND" fi CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS" shift fi #JPDA 模式是開啟遠(yuǎn)程debug模式,端口就是JPDA_ADDRESS,生產(chǎn)環(huán)境用不到,不去深入 #這個(gè)是debug模式 #值得注意的是變量$_RUNJDB,居然在全文沒有找到賦值的語(yǔ)句。 #從這邊開始就注意了,里面的if-fi判斷很多,一定要先調(diào)好格式再看 if [ "$1" = "debug" ] ; then if $os400; then echo "Debug command not available on OS400" exit 1 else shift if [ "$1" = "-security" ] ; then if [ $have_tty -eq 1 ]; then echo "Using Security Manager" fi shift exec "$_RUNJDB" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -sourcepath "$CATALINA_HOME"/../../java \ -Djava.security.manager \ -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" start else exec "$_RUNJDB" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -sourcepath "$CATALINA_HOME"/../../java \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" start fi fi #上面語(yǔ)句是適配AIX環(huán)境的,不做解讀 #當(dāng)參數(shù)是debug時(shí) elif [ "$1" = "run" ]; then shift #當(dāng)使用shift命令之后,原來(lái)的$2會(huì)變成$1,并且原有的$1變得不可用,通過$#命令獲得的參數(shù)個(gè)數(shù)也會(huì)少1 #這樣就可以使用$1獲取第二個(gè)參數(shù) if [ "$1" = "-security" ] ; then if [ $have_tty -eq 1 ]; then echo "Using Security Manager" fi shift #作廢掉前兩個(gè)參數(shù) exec "$_RUNJAVA" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -Djava.security.manager \ -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" start else exec "$_RUNJAVA" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" start #這個(gè)命令其實(shí)就是java命令,指定org.apache.catalina.startup.Bootstrap 這個(gè)class文件,$@和"start"作用類似于傳參到前面的安格class里 fi #正式進(jìn)入啟動(dòng)的過程啦~ elif [ "$1" = "start" ] ; then if [ ! -z "$CATALINA_PID" ]; then #test –z 字符串 字符串的長(zhǎng)度為零 ,判斷這個(gè)變量或者文件內(nèi)有數(shù)據(jù) if [ -f "$CATALINA_PID" ]; then #-f 該『文件名』是否為文件(file)?(常用) if [ -s "$CATALINA_PID" ]; then #-s 偵測(cè)該文件名是否為『非空白文件』? echo "Existing PID file found during start." if [ -r "$CATALINA_PID" ]; then #-r 偵測(cè)該文件名是否具有『可讀』的屬性? #以上if語(yǔ)句主要判斷這個(gè)$CATALINA_PID,是否能正常可讀 PID=`cat "$CATALINA_PID"` ps -p $PID >/dev/null 2>&1 if [ $? -eq 0 ] ; then #判斷這個(gè)PID現(xiàn)在有沒有被占用,有就輸出這個(gè)tomcat依舊在運(yùn)行,退出 #上面這段語(yǔ)句功能非常好,可以判斷出這個(gè)PID是否還在運(yùn)行 echo "Tomcat appears to still be running with PID $PID. Start aborted." exit 1 else echo "Removing/clearing stale PID file." rm -f "$CATALINA_PID" >/dev/null 2>&1 #刪除了這個(gè)$CATALINA_PID文件 if [ $? != 0 ]; then if [ -w "$CATALINA_PID" ]; then #-w 偵測(cè)該文件名是否具有『可寫』的屬性? cat /dev/null > "$CATALINA_PID" #清空$CATALINA_PID文件 else #如果不具有可寫權(quán)限,則輸出信息,退出 echo "Unable to remove or clear stale PID file. Start aborted." exit 1 fi fi fi else #如果"$CATALINA_PID"沒有可讀權(quán)限,任然輸出信息退出。 echo "Unable to read PID file. Start aborted." exit 1 fi else #"$CATALINA_PID"是空白文件 rm -f "$CATALINA_PID" >/dev/null 2>&1 #強(qiáng)制刪除$CATALINA_PID文件 if [ $? != 0 ]; then #成功則進(jìn)行判斷,沒有讀權(quán)限則退出腳本 if [ ! -w "$CATALINA_PID" ]; then echo "Unable to remove or write to empty PID file. Start aborted." exit 1 fi fi fi fi fi #這段語(yǔ)句,主要目的就是清空$CATALINA_PID文件,如果出現(xiàn)不可讀寫,則輸出信息退出 shift #作廢掉前面參數(shù),$1將表示第二個(gè)參數(shù) touch "$CATALINA_OUT" #創(chuàng)建$CATALINA_OUT變量路徑文件 if [ "$1" = "-security" ] ; then #如果第二個(gè)參數(shù)是-security if [ $have_tty -eq 1 ]; then echo "Using Security Manager" fi shift "$_RUNJAVA" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -Djava.security.manager \ -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" start \ >> "$CATALINA_OUT" 2>&1 & else #我們平時(shí)執(zhí)行啟動(dòng)tomcat核心就是這一句了 # "$_RUNJAVA" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" start \ >> "$CATALINA_OUT" 2>&1 & #從&可以看出啟動(dòng)的命令在后臺(tái)啟動(dòng) fi if [ ! -z "$CATALINA_PID" ]; then #判斷CATALINA_PID如果不是空字符,則將Shell最后運(yùn)行的后臺(tái)Process的PID 傳給$CATALINA_PID echo $! > "$CATALINA_PID" #在使用命令運(yùn)行進(jìn)程至后臺(tái)時(shí),可以使用$!抓取前面啟動(dòng)運(yùn)行在后臺(tái)進(jìn)程的進(jìn)程號(hào) fi fi #下面開始了tomcat停止模塊了 elif [ "$1" = "stop" ] ; then shift SLEEP=5 if [ ! -z "$1" ]; then echo $1 | grep "[^0-9]" >/dev/null 2>&1 if [ $? -gt 0 ]; then SLEEP=$1 shift fi fi #上面語(yǔ)句主要是判斷停止語(yǔ)句執(zhí)行到此時(shí),設(shè)定幾秒后再執(zhí)行停止語(yǔ)句 #用來(lái)配合stop n FORCE=0 if [ "$1" = "-force" ]; then shift FORCE=1 fi #如果參數(shù)中使用了-force,則FORCE=1 if [ ! -z "$CATALINA_PID" ]; then #$CATALINA_PID文件不是非空 if [ -f "$CATALINA_PID" ]; then if [ -s "$CATALINA_PID" ]; then kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1 #kill -0 pid 不發(fā)送任何信號(hào),但是系統(tǒng)會(huì)進(jìn)行錯(cuò)誤檢查。 if [ $? -gt 0 ]; then echo "PID file found but no matching process was found. Stop aborted." exit 1 fi else echo "PID file is empty and has been ignored." fi else echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted." exit 1 fi fi #以上腳本是進(jìn)行停止命令檢錯(cuò)的,如果可能停止不了,則可以直接報(bào)錯(cuò) "$_RUNJAVA" $JAVA_OPTS \ -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \ -Dcatalina.base="$CATALINA_BASE" \ -Dcatalina.home="$CATALINA_HOME" \ -Djava.io.tmpdir="$CATALINA_TMPDIR" \ org.apache.catalina.startup.Bootstrap "$@" stop #這個(gè)就是停止腳本的核心命令了 if [ ! -z "$CATALINA_PID" ]; then if [ -f "$CATALINA_PID" ]; then while [ $SLEEP -ge 0 ]; do kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1 if [ $? -gt 0 ]; then rm -f "$CATALINA_PID" >/dev/null 2>&1 if [ $? != 0 ]; then if [ -w "$CATALINA_PID" ]; then cat /dev/null > "$CATALINA_PID" else echo "Tomcat stopped but the PID file could not be removed or cleared." fi fi break fi if [ $SLEEP -gt 0 ]; then sleep 1 fi if [ $SLEEP -eq 0 ]; then if [ $FORCE -eq 0 ]; then echo "Tomcat did not stop in time. PID file was not removed." fi fi SLEEP=`expr $SLEEP - 1 ` done fi fi #上段語(yǔ)句主要是清空$CATALINA_PID #值得注意的是,生產(chǎn)環(huán)境偶爾不加-force選項(xiàng),tomcat有時(shí)無(wú)法停止下來(lái) #如果參數(shù)帶"-force" ,則強(qiáng)制kill掉tomcat if [ $FORCE -eq 1 ]; then if [ -z "$CATALINA_PID" ]; then echo "Kill failed: \$CATALINA_PID not set" else if [ -f "$CATALINA_PID" ]; then PID=`cat "$CATALINA_PID"` echo "Killing Tomcat with the PID: $PID" kill -9 $PID #強(qiáng)制執(zhí)行的核心命令 rm -f "$CATALINA_PID" >/dev/null 2>&1 if [ $? != 0 ]; then echo "Tomcat was killed but the PID file could not be removed." fi fi fi fi #查看版本參數(shù) elif [ "$1" = "version" ] ; then "$_RUNJAVA" \ -classpath "$CATALINA_HOME/lib/catalina.jar" \ org.apache.catalina.util.ServerInfo else echo "Usage: catalina.sh ( commands ... )" echo "commands:" if $os400; then echo " debug Start Catalina in a debugger (not available on OS400)" echo " debug -security Debug Catalina with a security manager (not available on OS400)" else echo " debug Start Catalina in a debugger" echo " debug -security Debug Catalina with a security manager" fi echo " jpda start Start Catalina under JPDA debugger" echo " run Start Catalina in the current window" echo " run -security Start in the current window with security manager" echo " start Start Catalina in a separate window" echo " start -security Start in a separate window with security manager" echo " stop Stop Catalina, waiting up to 5 seconds for the process to end" echo " stop n Stop Catalina, waiting up to n seconds for the process to end" echo " stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running" echo " stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running" echo " version What version of tomcat are you running?" echo "Note: Waiting for the process to end and use of the -force option require that \$CATALINA_PID is defined" exit 1 fi
總結(jié)下,catalina.sh腳本使用了大量的判斷,使用if作為參數(shù)的輸入判斷,核心的啟動(dòng)命令其實(shí)就是java命令。如果在發(fā)生tomcat啟動(dòng)異常,大家就可以排查時(shí)在catalina.sh完成時(shí)出現(xiàn)問題,還是catalina.sh啟動(dòng)后出現(xiàn)異常,如果有想要更加深入的小伙伴,可以學(xué)習(xí)下java,會(huì)在JVM調(diào)優(yōu)和排障上會(huì)走的更遠(yuǎn)。