tomcat变量环境脚本setclasspath.sh分析
????之所以分析setclasspath.sh腳本,是因?yàn)閏atalina.sh腳本會(huì)引用到這個(gè)腳本,如果不對(duì)其進(jìn)行分析,之后看catalina.sh腳本就會(huì)不知道一些變量沒有申明和賦值怎么會(huì)跑出來,本篇文章是為下一篇“tomcat運(yùn)行控制腳本catalina.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. #?----------------------------------------------------------------------------- #??Set?CLASSPATH?and?Java?options # #??$Id:?setclasspath.sh?795037?2009-07-17?10:52:16Z?markt?$ #?----------------------------------------------------------------------------- #?Make?sure?prerequisite?environment?variables?are?set #因?yàn)閟etclasspath.sh腳本是被catalina.sh調(diào)用,所以可以繼承catalina.sh中的變量申明 if?[?-z?"$JAVA_HOME"?-a?-z?"$JRE_HOME"?];?then #判斷用戶有沒有提前做$JAVA_HOME和$JRE_HOME全局變量聲明,如果都沒進(jìn)行申明#?Bugzilla?37284?(reviewed).if?$darwin;?then#要理解這個(gè)判斷,先看下startup.sh和shutdown.sh就會(huì)明白#這個(gè)是win仿真unix不用管下面兩個(gè)語(yǔ)句if?[?-d?"/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"?];?thenexport?JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"fielse#其他環(huán)境沒有申明,那么系統(tǒng)自己想辦法找這兩個(gè)變量的路徑JAVA_PATH=`which?java?2>/dev/null` #此語(yǔ)句可以把java命令位置找出來if?[?"x$JAVA_PATH"?!=?"x"?];?then #如果能找出java路徑,則可以定位到j(luò)ava命令的路徑,經(jīng)過作者驗(yàn)證不是java的裝路徑 #所以通過此處就可以看出,老鳥們?yōu)槭裁炊家约褐付ㄟ@兩個(gè)變量了JAVA_PATH=`dirname?$JAVA_PATH?2>/dev/null`JRE_HOME=`dirname?$JAVA_PATH?2>/dev/null`fiif?[?"x$JRE_HOME"?=?"x"?];?then #如果找不到j(luò)ava路徑,那么就看有沒有/usr/bin/java這個(gè)執(zhí)行文件,有的話就它了,沒有就算了#?XXX:?Should?we?try?other?locations?if?[?-x?/usr/bin/java?];?thenJRE_HOME=/usrfififiif?[?-z?"$JAVA_HOME"?-a?-z?"$JRE_HOME"?];?then#再驗(yàn)證一邊,有沒有這兩個(gè)變量,沒有不好意思,我不執(zhí)行了,退出#這個(gè)exit?1?不但是結(jié)束setclasspath.sh,會(huì)徹底退出catalina.sh腳本的#對(duì)于在腳本中引用腳本的童鞋們,就需要注意了,小心使用exit。?echo?"Neither?the?JAVA_HOME?nor?the?JRE_HOME?environment?variable?is?defined"echo?"At?least?one?of?these?environment?variable?is?needed?to?run?this?program"exit?1fi fi if?[?-z?"$JAVA_HOME"?-a?"$1"?=?"debug"?];?thenecho?"JAVA_HOME?should?point?to?a?JDK?in?order?to?run?in?debug?mode."exit?1 fi if?[?-z?"$JRE_HOME"?];?thenJRE_HOME="$JAVA_HOME" fi #?If?we're?running?under?jdb,?we?need?a?full?jdk. if?[?"$1"?=?"debug"?]?;?thenif?[?"$os400"?=?"true"?];?thenif?[?!?-x?"$JAVA_HOME"/bin/java?-o?!?-x?"$JAVA_HOME"/bin/javac?];?thenecho?"The?JAVA_HOME?environment?variable?is?not?defined?correctly"echo?"This?environment?variable?is?needed?to?run?this?program"echo?"NB:?JAVA_HOME?should?point?to?a?JDK?not?a?JRE"exit?1fielseif?[?!?-x?"$JAVA_HOME"/bin/java?-o?!?-x?"$JAVA_HOME"/bin/jdb?-o?!?-x?"$JAVA_HOME"/bin/javac?];?thenecho?"The?JAVA_HOME?environment?variable?is?not?defined?correctly"echo?"This?environment?variable?is?needed?to?run?this?program"echo?"NB:?JAVA_HOME?should?point?to?a?JDK?not?a?JRE"exit?1fifi fi #上段的代碼都是在確認(rèn)$JAVA_HOME和$JRE_HOME變量的申明情況及后續(xù)的解決過程 if?[?-z?"$BASEDIR"?];?then #對(duì)"$BASEDIR變量的檢查,木有的話就退出echo?"The?BASEDIR?environment?variable?is?not?defined"echo?"This?environment?variable?is?needed?to?run?this?program"exit?1 fi if?[?!?-x?"$BASEDIR"/bin/setclasspath.sh?];?then #確認(rèn)"$BASEDIR"/bin/setclasspath.sh有木有,木有還是退出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?groupseval #eval不清楚嘛意思else?echo?"The?BASEDIR?environment?variable?is?not?defined?correctly"echo?"This?environment?variable?is?needed?to?run?this?program"exit?1fi fi #?Don't?override?the?endorsed?dir?if?the?user?has?set?it?previously #這個(gè)是確認(rèn)JAVA_ENDORSED_DIRS的位置 if?[?-z?"$JAVA_ENDORSED_DIRS"?];?then#?Set?the?default?-Djava.endorsed.dirs?argumentJAVA_ENDORSED_DIRS="$BASEDIR"/endorsed fi #?OSX?hack?to?CLASSPATH JIKESPATH= if?[?`uname?-s`?=?"Darwin"?];?thenOSXHACK="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Classes"if?[?-d?"$OSXHACK"?];?thenfor?i?in?"$OSXHACK"/*.jar;?doJIKESPATH="$JIKESPATH":"$i"donefi fi #?Set?standard?commands?for?invoking?Java. #這句是響當(dāng)當(dāng)?shù)闹匾?#xff0c;確定了$_RUNJAVA的值 _RUNJAVA="$JRE_HOME"/bin/java if?[?"$os400"?!=?"true"?];?then_RUNJDB="$JAVA_HOME"/bin/jdb fi????通過腳本分析,我們就看到,這個(gè)腳本就做了一件事情,檢查各種變量是否賦值,驗(yàn)證tomcat啟動(dòng)停止需要涉及到的文件,保障tomcat順利啟動(dòng)停止。平時(shí)我們想要學(xué)習(xí)shell實(shí)例,但是網(wǎng)上卻很少,這是可以轉(zhuǎn)變下思路,我們linux系統(tǒng)里,充斥著大量的shell實(shí)例,雖然不能拿來直接用,但是這些shell的嚴(yán)謹(jǐn)代碼結(jié)構(gòu)、邏輯思路、其中的亮點(diǎn)都可以借鑒來使用。
????需要補(bǔ)充的是,當(dāng)參數(shù)選擇catalina.sh stop -force時(shí),catalina.sh是先執(zhí)行catalina.sh stop,以正常的java命令stop掉tomcat,當(dāng)加入-force時(shí),如果正常關(guān)閉不起作用,則執(zhí)行強(qiáng)制關(guān)閉。
轉(zhuǎn)載于:https://blog.51cto.com/vekergu/1622244
總結(jié)
以上是生活随笔為你收集整理的tomcat变量环境脚本setclasspath.sh分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信号量(一) 基础知识
- 下一篇: 朋友圈玩分身术!逼死用户的节奏?