1. 程式人生 > >Tomcat7源碼分析學習系列之一-----tomcat的啟動文件startup的註釋

Tomcat7源碼分析學習系列之一-----tomcat的啟動文件startup的註釋

addition mission etl %0 很好 fine copy 文件結尾 存在

1. Windows系統,tomcat啟動文件startup.bat

@echo off       rem 關閉回顯,不顯示下面的命令;
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0


rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,

rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server


rem ---------------------------------------------------------------------------

setlocal                                   rem setlocal命令將啟動批處理文件中環境變量的本地化。本地化將持續到出現匹配的 endlocal 命令或者到達批處理文件結尾為止

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"                        rem 設置環境變量CURRENT_DIR為當前目錄--G:\server\apache-tomcat-7.0.75-src\bin
if not "%CATALINA_HOME%" == "" goto gotHome            rem 如果設置了環境變量CATALINA_HOME,即CATALINA_HOME不為空,轉到 :gotHome
set "CATALINA_HOME=%CURRENT_DIR%"                rem 沒有設置環境變量CATALINA_HOME,則設置CATALINA_HOME=CURRENT_DIR,即當前文件所在目錄
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome       rem 如果"%CATALINA_HOME%\bin\catalina.bat"存在,則跳轉到:okHome
cd ..                                   rem 如果"%CATALINA_HOME%\bin\catalina.bat"不存在,回到當前目錄的上級目錄
set "CATALINA_HOME=%cd%"                      rem 設置CATALINA_HOME為當前目錄,即startup.bat文件所在目錄的上級目錄
cd "%CURRENT_DIR%"                          rem 進入到環境變量CURRENT_DIR指定的目錄--G:\server\apache-tomcat-7.0.75-src\bin
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly rem 如果"%CATALINA_HOME%\bin\catalina.bat"不存在,提示CATALINA_HOME沒有正確定義,該環境變量是運行tomcat必須的
echo This environment variable is needed to run this program
goto end rem 跳轉到 :end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"        rem 設置環境變量EXECUTABLE=執行程序%CATALINA_HOME%\bin\catalina.bat

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec                 rem 如果EXECUTABLE存在,跳轉到:okExec
echo Cannot find "%EXECUTABLE%"                  rem 如果EXECUTABLE即%CATALINA_HOME%\bin\catalina.bat不存在,則提示不能找到環境變 rem 量%CATALINA_HOME%\bin\catalina.bat,
echo This file is needed to run this program               rem 這個文件是運行tomcat必須的
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=                          rem 設置命令行參數  
:setArgs
rem %[1-9]表示參數,參數是指在運行批處理文件時在文件名後面,以空格(或者Tab)分隔的字符串。變量可以從%0到%9,%0表示批處理命令本身,其它參數字符串用%1到%9順序表示。
if ""%1""=="""" goto doneSetArgs                      rem 如果沒有參數跳轉到:doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1               rem 設置CMD_LINE_ARGS=當前命令行參數 加上第一個參數(%1)
shift                                    rem 把第二個參數移動到第一位,即變量%2移到1%
goto setArgs                                rem 循環把所有參數都添加到CMD_LINE_ARGS
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%               rem 啟動EXECUTABLE即%CATALINA_HOME%\bin\catalina.bat ,參數為CMD_LINE_ARGS

:end

2.Linux/unix系統,tomcat啟動文件startup.sh

#!/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 Script for the CATALINA Server
# -----------------------------------------------------------------------------

# Better OS/400 detection: see Bugzilla 31132
os400=false                                    #os400 IBM的操作系統
case "`uname`" in                                #uname獲取操作系統名稱,如果獲取到的操作系統名稱,是以OS400開頭,這說明是IBM操作系統
OS400*) os400=true;;
esac

# resolve links - $0 may be a softlink
PRG="$0"                                  #[[email protected] Desktop]$ echo PRG="$0" PRG=/bin/bash,腳本本身的名字:$0,

#變量賦值,變量後面緊跟賦值符號"=",不能加空格,tab鍵等

while [ -h "$PRG" ] ; do                            #判斷變量$PRG是不是連接,是的話,循環直到找到文件地址
ls=`ls -ld "$PRG"`
link=`expr "$ls" : ‘.*-> \(.*\)$‘`
if expr "$link" : ‘/.*‘ > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
#上面循環語句的意思是保證文件路徑不是一個連接,使用循環直至找到文件原地址
#遇到一時看不明白的shell,可以拆解後自己在linux反復運行驗證,一點點拆解就會明白的,但是還是不是很明白
PRGDIR=`dirname "$PRG"`                              #$PRG相對路徑--.
EXECUTABLE=catalina.sh                             #賦值

# Check that target executable exists
if $os400; then
# -x 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
eval
else
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then                      #判斷腳本catalina.sh是否存在並有可執行權限,沒有執行權限就退出
echo "Cannot find $PRGDIR/$EXECUTABLE"
echo "The file is absent or does not have execute permission"
echo "This file is needed to run this program"
exit 1
fi
fi

exec "$PRGDIR"/"$EXECUTABLE" start "$@"                   #./catalina.sh start +輸入參數

#exec命令在執行時會把當前的shell process關閉,然後換到後面的命令繼續執行。
#exec命令可以很好的進行腳本之間過渡,並且結束掉前一個腳本這樣不會對後面執行的腳本造成幹擾。
#exec 命令:常用來替代當前 shell 並重新啟動一個 shell,換句話說,並沒有啟動子 shell。使用這一命令時任何現有環境都將會被清除。exec 也只有在對文件描述符進行操作的時候,exec 不會覆蓋你當前的 shell 環境。
#exec 可以用於腳本執行完啟動需要啟動另一個腳本時使用,但須考慮到環境變量是否被繼承

3.總結

startup.bat和startup.sh腳本實際上並沒有做什麽具體工作,主要功能是在從tomcat目錄下找到正確的catalina.bat/catalina.sh,並調用catalina.bat /catalina.sh start命令 +命令行中的參數。

即startup.bat/startup.sh的主要作用是調用catalina.bat/catalina.sh start命令(即其與命令行中執行catalina.bat/cata.ina.sh start作用一樣)。

Tomcat7源碼分析學習系列之一-----tomcat的啟動文件startup的註釋