1. 程式人生 > >tomcat 9.0.11 原始碼分析--總結startup.sh與catalina.sh

tomcat 9.0.11 原始碼分析--總結startup.sh與catalina.sh

startup.sh與catalina.sh只是啟動指令碼,通過環境變數與指令碼所在目錄確定java的啟動引數來正確啟動tomcat, 從引數可以找到啟動類是org.apache.catalina.startup.Bootstrap裡的main方法

指令碼中常用判斷
if [ -z “$UMASK” ] 類似這樣就是UMASK環境變數不存在時設定初始化

if [ -r “$CATALINA_HOME”/bin/setclasspath.sh ] 這是判斷檔案存在且可讀

if [ -n “$JAVA_ENDORSED_DIRS” ] 存在環境變數時處理

if [ -d “$CATALINA_HOME/endorsed” ] 是目錄處理

while [ -h “$PRG” ] 是軟連線處理

dirname “$PRG” 取得檔案所在目錄,PRG問檔案

列印啟動引數
在catalina.sh的最後處理start引數那裡,新增一系列的echo 列印輸出引數

elif [ "$1" = "start" ] ; then

  if [ ! -z "$CATALINA_PID" ]; then
    if [ -f "$CATALINA_PID" ]; then
      if [ -s "$CATALINA_PID" ]; then
        echo "Existing PID file found during start."
if [ -r "$CATALINA_PID" ]; then PID=`cat "$CATALINA_PID"` ps -p $PID >/dev/null 2>&1 if [ $? -eq 0 ] ; then echo "Tomcat appears to still be running with PID $PID. Start aborted." echo "If the following process is not a Tomcat process, remove the PID file and try again:"
ps -f -p $PID exit 1 else echo "Removing/clearing stale PID file." rm -f "$CATALINA_PID" >/dev/null 2>&1 if [ $? != 0 ]; then if [ -w "$CATALINA_PID" ]; then cat /dev/null > "$CATALINA_PID" else echo "Unable to remove or clear stale PID file. Start aborted." exit 1 fi fi fi else echo "Unable to read PID file. Start aborted." exit 1 fi else rm -f "$CATALINA_PID" >/dev/null 2>&1 if [ $? != 0 ]; then if [ ! -w "$CATALINA_PID" ]; then echo "Unable to remove or write to empty PID file. Start aborted." exit 1 fi fi fi fi fi shift touch "$CATALINA_OUT" if [ "$1" = "-security" ] ; then if [ $have_tty -eq 1 ]; then echo "Using Security Manager" fi shift eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ -D$ENDORSED_PROP="\"$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 "[email protected]" start \ >> "$CATALINA_OUT" 2>&1 "&" else echo "_NOHUP:" $_NOHUP echo "_RUNJAVA:" $_RUNJAVA echo "LOGGING_CONFIG:" $LOGGING_CONFIG echo "LOGGING_MANAGER:" $LOGGING_MANAGER echo "JAVA_OPTS:" $JAVA_OPTS echo "CATALINA_OPTS:" $CATALINA_OPTS echo "ENDORSED_PROP:" $ENDORSED_PROP echo "JAVA_ENDORSED_DIRS:" $JAVA_ENDORSED_DIRS echo "CLASSPATH:" $CLASSPATH echo "CATALINA_BASE:" $CATALINA_BASE echo "CATALINA_HOME:" $CATALINA_HOME echo "CATALINA_TMPDIR:" $CATALINA_TMPDIR echo "CATALINA_OUT:" $CATALINA_OUT echo "print start parameters" exit 0 eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ -classpath "\"$CLASSPATH\"" \ -Dcatalina.base="\"$CATALINA_BASE\"" \ -Dcatalina.home="\"$CATALINA_HOME\"" \ -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ org.apache.catalina.startup.Bootstrap "[email protected]" start \ >> "$CATALINA_OUT" 2>&1 "&" fi if [ ! -z "$CATALINA_PID" ]; then echo $! > "$CATALINA_PID" fi echo "Tomcat started." elif [ "$1" = "stop" ] ; then

得到引數後,可以自己在命令列直接 java 組裝引數 執行tomcat

//這是組裝引數的最後,[email protected]表示所有傳給catalina.sh的引數
//startup.sh傳給catalina.sh start  catalina.sh在處理了第一個引數start
後使用shift,移除了第一個引數,所以下面不會有兩個start
org.apache.catalina.startup.Bootstrap "[email protected]" start \
      >> "$CATALINA_OUT" 2>&1 "&"

startup.sh 就是找到catalina.sh的目錄,並加引數start執行

./catalina.sh start 因為執行的命令是: java 引數 & 所以會後臺執行
./catalina.sh run 可以在終端執行,看日誌
./catalina.sh 沒有引數列印幫助文件

下面是我整理的啟動引數,mac系統,路徑可能需要修改,umask值修改

java -Djava.util.logging.config.file=/Users/Downloads/apache-tomcat-9.0.11/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs="" -classpath "/Users/Downloads/apache-tomcat-9.0.11/bin/bootstrap.jar:/Users/Downloads/apache-tomcat-9.0.11/bin/tomcat-juli.jar" -Dcatalina.base="/Users/Downloads/apache-tomcat-9.0.11" -Dcatalina.home="/Users/Downloads/apache-tomcat-9.0.11" -Djava.io.tmpdir="/Users/Downloads/apache-tomcat-9.0.11/temp" org.apache.catalina.startup.Bootstrap start >>/Users/Downloads/apache-tomcat-9.0.11/logs/catalina.out 2>&1

自定義環境變數,可以編寫setenv.sh,放到CATALINA_BASE/bin下面,catalina.sh啟動會載入,比如要載入的類jar包等等,全域性

CLASSPATH=

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