Tomcat启动脚本startup.sh分析
一、分析說明
? ? 為了寫出更加完善的tomcat啟動方面的自動化腳本,健壯自己用于代碼上線自動化部署的腳本,特分析下tomcat的bin目錄下的starup.sh腳本,學(xué)習(xí)標(biāo)準(zhǔn)的sh腳本的編寫方法,從中吸取經(jīng)驗
二、腳本分析
#!/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 操作環(huán)境的操作系統(tǒng)成份?
#Darwin是windows平臺上運行的類UNIX模擬環(huán)境?
case "`uname`" in
CYGWIN*) cygwin=true;;?
OS400*) os400=true;;?
Darwin*) darwin=true;;?
esac
#上一個判斷是為了判斷操作系統(tǒng),至于何用,往下看?
# 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
#上面循環(huán)語句的意思是保證文件路徑不是一個連接,使用循環(huán)直至找到文件原地址?
#遇到一時看不明白的shell,可以拆解后自己在linux反復(fù)運行驗證,一點點拆解就會明白的?
#link=`expr "$ls" : '.*-> \(.*\)$'` 模擬后: expr 'lrwxrwxrwx 1 root root 19 3月? 17 10:12 ./bbb.sh -> /root/shell/test.sh' : '.*-> \(.*\)$'?
#很明確的發(fā)現(xiàn)是用expr來提取/root/shell/test.sh的內(nèi)容?
#而這個循環(huán)就可以明確其目的,排除命令為鏈接,找出命令真正的目錄,防止后面的命令出錯??
#這段代碼如果在以后有這方面的找出鏈接源頭的需求可以完全借鑒?
??
#獲取這個腳本的目錄?
PRGDIR=`dirname "$PRG"`?
EXECUTABLE=catalina.sh?
# Check that target executable exists?
#這些判斷是否氣是其他的操作系統(tǒng)?
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是否存在并有可執(zhí)行權(quán)限,沒有執(zhí)行權(quán)限就退出??
? ? 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命令在執(zhí)行時會把當(dāng)前的shell process關(guān)閉,然后換到后面的命令繼續(xù)執(zhí)行。?
#exec命令可以很好的進(jìn)行腳本之間過渡,并且結(jié)束掉前一個腳本這樣不會對后面執(zhí)行的腳本造成干擾。?
#exec 命令:常用來替代當(dāng)前 shell 并重新啟動一個 shell,換句話說,并沒有啟動子 shell。使用這一命令時任何現(xiàn)?
#有環(huán)境都將會被清除。exec 在對文件描述符進(jìn)行操作的時候,也只有在這時,exec 不會覆蓋你當(dāng)前的 shell 環(huán)境。?
#exec 可以用于腳本執(zhí)行完啟動需要啟動另一個腳本是使用,但須考慮到環(huán)境變量是否被繼承。
三、總結(jié)
? ? tomcat的startup.sh腳本主要用來判斷環(huán)境,找到catalina.sh腳本源路徑,將啟動命令參數(shù)傳遞給catalina.sh執(zhí)行。然而catalina.sh腳本中也涉及到判斷系統(tǒng)環(huán)境和找到catalina.sh腳本原路徑的相關(guān)代碼,所以執(zhí)行tomcat啟動時,無需使用startup.sh腳本(下一篇分析的shutdown.sh也類似,見 ),直接./catalina.sh start|stop|restart 即可。
轉(zhuǎn)載于:https://blog.51cto.com/felixzhang/1855486
總結(jié)
以上是生活随笔為你收集整理的Tomcat启动脚本startup.sh分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IEnumerable 使用foreac
- 下一篇: 〔译〕TypeScript 2.0 正式