Tomcat启动脚本startup.sh分析
一、分析說明
? ? 為了寫出更加完善的tomcat啟動方面的自動化腳本,健壯自己用于代碼上線自動化部署的腳本,特分析下tomcat的bin目錄下的starup.sh腳本,學習標準的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?
#?
# $Id: startup.sh 1130937 2011-06-03 08:27:13Z markt $?
# -----------------------------------------------------------------------------?
# Better OS/400 detection: see Bugzilla 31132?
os400=false
darwin=false
#os400是 IBM的AIX?
#darwin是MacOSX 操作環境的操作系統成份?
#Darwin是windows平臺上運行的類UNIX模擬環境?
case "`uname`" in
CYGWIN*) cygwin=true;;?
OS400*) os400=true;;?
Darwin*) darwin=true;;?
esac
#上一個判斷是為了判斷操作系統,至于何用,往下看?
# resolve links - $0 may be a softlink?
#讀取腳本名?
PRG="$0"
#test –h File 文件存在并且是一個符號鏈接(同-L)?
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
#上面循環語句的意思是保證文件路徑不是一個連接,使用循環直至找到文件原地址?
#遇到一時看不明白的shell,可以拆解后自己在linux反復運行驗證,一點點拆解就會明白的?
#link=`expr "$ls" : '.*-> \(.*\)$'` 模擬后: expr 'lrwxrwxrwx 1 root root 19 3月? 17 10:12 ./bbb.sh -> /root/shell/test.sh' : '.*-> \(.*\)$'?
#很明確的發現是用expr來提取/root/shell/test.sh的內容?
#而這個循環就可以明確其目的,排除命令為鏈接,找出命令真正的目錄,防止后面的命令出錯??
#這段代碼如果在以后有這方面的找出鏈接源頭的需求可以完全借鑒?
??
#獲取這個腳本的目錄?
PRGDIR=`dirname "$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
? #這個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 "$@"
#exec命令在執行時會把當前的shell process關閉,然后換到后面的命令繼續執行。?
#exec命令可以很好的進行腳本之間過渡,并且結束掉前一個腳本這樣不會對后面執行的腳本造成干擾。?
#exec 命令:常用來替代當前 shell 并重新啟動一個 shell,換句話說,并沒有啟動子 shell。使用這一命令時任何現?
#有環境都將會被清除。exec 在對文件描述符進行操作的時候,也只有在這時,exec 不會覆蓋你當前的 shell 環境。?
#exec 可以用于腳本執行完啟動需要啟動另一個腳本是使用,但須考慮到環境變量是否被繼承。
三、總結
? ? tomcat的startup.sh腳本主要用來判斷環境,找到catalina.sh腳本源路徑,將啟動命令參數傳遞給catalina.sh執行。然而catalina.sh腳本中也涉及到判斷系統環境和找到catalina.sh腳本原路徑的相關代碼,所以執行tomcat啟動時,無需使用startup.sh腳本(下一篇分析的shutdown.sh也類似,見 ),直接./catalina.sh start|stop|restart 即可。
轉載于:https://blog.51cto.com/felixzhang/1855486
總結
以上是生活随笔為你收集整理的Tomcat启动脚本startup.sh分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IEnumerable 使用foreac
- 下一篇: 〔译〕TypeScript 2.0 正式