[关闭]
@cyysu 2017-10-12T10:19:14.000000Z 字数 11685 阅读 827

Bat-实战基础篇(四)

  • 时间:2017年10月12日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
  • 版本:4.0
  • 描述:批处理实战基础篇四,主要讲解tomcat下启动脚本,本文主要讲解catalina.bat这个脚本

Bat系列教程


项目实战

其目录下面的脚本文件如下,有些重复的内容,这里不做过多介绍,有遇到什么不会的内容可以查看Bat系列教程

下面我们一一来分析其里面的内容,为了避免文本过长,每个文件单独一篇文章讲解。

  1. # catalina.bat
  2. :: 我们重点讲解bat语法以及模仿别人如何编写,从他人代码中丰富自己,不对脚本中的逻辑关系进行过多介绍。
  3. @echo off
  4. rem Licensed to the Apache Software Foundation (ASF) under one or more
  5. rem contributor license agreements. See the NOTICE file distributed with
  6. rem this work for additional information regarding copyright ownership.
  7. rem The ASF licenses this file to You under the Apache License, Version 2.0
  8. rem (the "License"); you may not use this file except in compliance with
  9. rem the License. You may obtain a copy of the License at
  10. rem
  11. rem http://www.apache.org/licenses/LICENSE-2.0
  12. rem
  13. rem Unless required by applicable law or agreed to in writing, software
  14. rem distributed under the License is distributed on an "AS IS" BASIS,
  15. rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. rem See the License for the specific language governing permissions and
  17. rem limitations under the License.
  18. rem ---------------------------------------------------------------------------
  19. rem Start/Stop Script for the CATALINA Server
  20. rem
  21. rem Environment Variable Prerequisites
  22. rem
  23. rem Do not set the variables in this script. Instead put them into a script
  24. rem setenv.bat in CATALINA_BASE/bin to keep your customizations separate.
  25. rem
  26. rem WHEN RUNNING TOMCAT AS A WINDOWS SERVICE:
  27. rem Note that the environment variables that affect the behavior of this
  28. rem script will have no effect at all on Windows Services. As such, any
  29. rem local customizations made in a CATALINA_BASE/bin/setenv.bat script
  30. rem will also have no effect on Tomcat when launched as a Windows Service.
  31. rem The configuration that controls Windows Services is stored in the Windows
  32. rem Registry, and is most conveniently maintained using the "tomcatXw.exe"
  33. rem maintenance utility, where "X" is the major version of Tomcat you are
  34. rem running.
  35. rem
  36. rem CATALINA_HOME May point at your Catalina "build" directory.
  37. rem
  38. rem CATALINA_BASE (Optional) Base directory for resolving dynamic portions
  39. rem of a Catalina installation. If not present, resolves to
  40. rem the same directory that CATALINA_HOME points to.
  41. rem
  42. rem CATALINA_OPTS (Optional) Java runtime options used when the "start",
  43. rem "run" or "debug" command is executed.
  44. rem Include here and not in JAVA_OPTS all options, that should
  45. rem only be used by Tomcat itself, not by the stop process,
  46. rem the version command etc.
  47. rem Examples are heap size, GC logging, JMX ports etc.
  48. rem
  49. rem CATALINA_TMPDIR (Optional) Directory path location of temporary directory
  50. rem the JVM should use (java.io.tmpdir). Defaults to
  51. rem %CATALINA_BASE%\temp.
  52. rem
  53. rem JAVA_HOME Must point at your Java Development Kit installation.
  54. rem Required to run the with the "debug" argument.
  55. rem
  56. rem JRE_HOME Must point at your Java Runtime installation.
  57. rem Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
  58. rem are both set, JRE_HOME is used.
  59. rem
  60. rem JAVA_OPTS (Optional) Java runtime options used when any command
  61. rem is executed.
  62. rem Include here and not in CATALINA_OPTS all options, that
  63. rem should be used by Tomcat and also by the stop process,
  64. rem the version command etc.
  65. rem Most options should go into CATALINA_OPTS.
  66. rem
  67. rem JAVA_ENDORSED_DIRS (Optional) Lists of of semi-colon separated directories
  68. rem containing some jars in order to allow replacement of APIs
  69. rem created outside of the JCP (i.e. DOM and SAX from W3C).
  70. rem It can also be used to update the XML parser implementation.
  71. rem Defaults to $CATALINA_HOME/endorsed.
  72. rem
  73. rem JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"
  74. rem command is executed. The default is "dt_socket".
  75. rem
  76. rem JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start"
  77. rem command is executed. The default is 8000.
  78. rem
  79. rem JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start"
  80. rem command is executed. Specifies whether JVM should suspend
  81. rem execution immediately after startup. Default is "n".
  82. rem
  83. rem JPDA_OPTS (Optional) Java runtime options used when the "jpda start"
  84. rem command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
  85. rem and JPDA_SUSPEND are ignored. Thus, all required jpda
  86. rem options MUST be specified. The default is:
  87. rem
  88. rem -agentlib:jdwp=transport=%JPDA_TRANSPORT%,
  89. rem address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
  90. rem
  91. rem JSSE_OPTS (Optional) Java runtime options used to control the TLS
  92. rem implementation when JSSE is used. Default is:
  93. rem "-Djdk.tls.ephemeralDHKeySize=2048"
  94. rem
  95. rem LOGGING_CONFIG (Optional) Override Tomcat's logging config file
  96. rem Example (all one line)
  97. rem set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"
  98. rem
  99. rem LOGGING_MANAGER (Optional) Override Tomcat's logging manager
  100. rem Example (all one line)
  101. rem set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
  102. rem
  103. rem TITLE (Optional) Specify the title of Tomcat window. The default
  104. rem TITLE is Tomcat if it's not specified.
  105. rem Example (all one line)
  106. rem set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%]
  107. rem ---------------------------------------------------------------------------
  108. setlocal
  109. :: 第一段
  110. rem Suppress Terminate batch job on CTRL+C
  111. if not ""%1"" == ""run"" goto mainEntry
  112. if "%TEMP%" == "" goto mainEntry
  113. if exist "%TEMP%\%~nx0.run" goto mainEntry
  114. :: %~nx0表示文件的名字 不带路径
  115. echo Y>"%TEMP%\%~nx0.run"
  116. :: 如果%TEMP%\catalina.bat.run 不存在 那么就跳转mainEntry
  117. if not exist "%TEMP%\%~nx0.run" goto mainEntry
  118. echo Y>"%TEMP%\%~nx0.Y"
  119. :: %~f0表示运行的文件全路径 %*代表所有参数
  120. call "%~f0" %* <"%TEMP%\%~nx0.Y"
  121. rem Use provided errorlevel
  122. set RETVAL=%ERRORLEVEL%
  123. del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1
  124. exit /B %RETVAL%
  125. :mainEntry
  126. del /Q "%TEMP%\%~nx0.run" >NUL 2>&1
  127. :: 第二段
  128. rem Guess CATALINA_HOME if not defined
  129. set "CURRENT_DIR=%cd%"
  130. if not "%CATALINA_HOME%" == "" goto gotHome
  131. set "CATALINA_HOME=%CURRENT_DIR%"
  132. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  133. cd ..
  134. set "CATALINA_HOME=%cd%"
  135. cd "%CURRENT_DIR%"
  136. :gotHome
  137. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  138. echo The CATALINA_HOME environment variable is not defined correctly
  139. echo This environment variable is needed to run this program
  140. goto end
  141. :okHome
  142. rem Copy CATALINA_BASE from CATALINA_HOME if not defined
  143. if not "%CATALINA_BASE%" == "" goto gotBase
  144. set "CATALINA_BASE=%CATALINA_HOME%"
  145. :gotBase
  146. rem Ensure that any user defined CLASSPATH variables are not used on startup,
  147. rem but allow them to be specified in setenv.bat, in rare case when it is needed.
  148. set CLASSPATH=
  149. rem Get standard environment variables
  150. if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
  151. call "%CATALINA_BASE%\bin\setenv.bat"
  152. goto setenvDone
  153. :checkSetenvHome
  154. if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
  155. :setenvDone
  156. rem Get standard Java environment variables
  157. if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
  158. echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
  159. echo This file is needed to run this program
  160. goto end
  161. :okSetclasspath
  162. call "%CATALINA_HOME%\bin\setclasspath.bat" %1
  163. :: 这个格式是if的特殊用法 if 检测执行是否正确,同时if还可以检测变量是否定义 采用 if defined
  164. if errorlevel 1 goto end
  165. rem Add on extra jar file to CLASSPATH
  166. rem Note that there are no quotes as we do not want to introduce random
  167. rem quotes into the CLASSPATH
  168. if "%CLASSPATH%" == "" goto emptyClasspath
  169. set "CLASSPATH=%CLASSPATH%;"
  170. :emptyClasspath
  171. set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
  172. if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
  173. set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
  174. :gotTmpdir
  175. rem Add tomcat-juli.jar to classpath
  176. rem tomcat-juli.jar can be over-ridden per instance
  177. if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
  178. set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
  179. goto juliClasspathDone
  180. :juliClasspathHome
  181. set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
  182. :juliClasspathDone
  183. if not "%JSSE_OPTS%" == "" goto gotJsseOpts
  184. set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
  185. :gotJsseOpts
  186. set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"
  187. if not "%LOGGING_CONFIG%" == "" goto noJuliConfig
  188. set LOGGING_CONFIG=-Dnop
  189. if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig
  190. set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
  191. :noJuliConfig
  192. if not "%LOGGING_MANAGER%" == "" goto noJuliManager
  193. set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
  194. :noJuliManager
  195. rem ----- Execute The Requested Command ---------------------------------------
  196. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  197. echo Using CATALINA_HOME: "%CATALINA_HOME%"
  198. echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
  199. if ""%1"" == ""debug"" goto use_jdk
  200. echo Using JRE_HOME: "%JRE_HOME%"
  201. goto java_dir_displayed
  202. :use_jdk
  203. echo Using JAVA_HOME: "%JAVA_HOME%"
  204. :java_dir_displayed
  205. echo Using CLASSPATH: "%CLASSPATH%"
  206. set _EXECJAVA=%_RUNJAVA%
  207. set MAINCLASS=org.apache.catalina.startup.Bootstrap
  208. set ACTION=start
  209. set SECURITY_POLICY_FILE=
  210. set DEBUG_OPTS=
  211. set JPDA=
  212. if not ""%1"" == ""jpda"" goto noJpda
  213. set JPDA=jpda
  214. if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
  215. set JPDA_TRANSPORT=dt_socket
  216. :gotJpdaTransport
  217. if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
  218. set JPDA_ADDRESS=8000
  219. :gotJpdaAddress
  220. if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
  221. set JPDA_SUSPEND=n
  222. :gotJpdaSuspend
  223. if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
  224. set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
  225. :gotJpdaOpts
  226. shift
  227. :noJpda
  228. if ""%1"" == ""debug"" goto doDebug
  229. if ""%1"" == ""run"" goto doRun
  230. if ""%1"" == ""start"" goto doStart
  231. if ""%1"" == ""stop"" goto doStop
  232. if ""%1"" == ""configtest"" goto doConfigTest
  233. if ""%1"" == ""version"" goto doVersion
  234. echo Usage: catalina ( commands ... )
  235. echo commands:
  236. echo debug Start Catalina in a debugger
  237. echo debug -security Debug Catalina with a security manager
  238. echo jpda start Start Catalina under JPDA debugger
  239. echo run Start Catalina in the current window
  240. echo run -security Start in the current window with security manager
  241. echo start Start Catalina in a separate window
  242. echo start -security Start in a separate window with security manager
  243. echo stop Stop Catalina
  244. echo configtest Run a basic syntax check on server.xml
  245. echo version What version of tomcat are you running?
  246. goto end
  247. :doDebug
  248. shift
  249. set _EXECJAVA=%_RUNJDB%
  250. set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
  251. if not ""%1"" == ""-security"" goto execCmd
  252. shift
  253. echo Using Security Manager
  254. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  255. goto execCmd
  256. :doRun
  257. shift
  258. if not ""%1"" == ""-security"" goto execCmd
  259. shift
  260. echo Using Security Manager
  261. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  262. goto execCmd
  263. :doStart
  264. shift
  265. if "%TITLE%" == "" set TITLE=Tomcat
  266. set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
  267. if not ""%1"" == ""-security"" goto execCmd
  268. shift
  269. echo Using Security Manager
  270. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  271. goto execCmd
  272. :doStop
  273. shift
  274. set ACTION=stop
  275. set CATALINA_OPTS=
  276. goto execCmd
  277. :doConfigTest
  278. shift
  279. set ACTION=configtest
  280. set CATALINA_OPTS=
  281. goto execCmd
  282. :doVersion
  283. %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
  284. goto end
  285. :execCmd
  286. rem Get remaining unshifted command line arguments and save them in the
  287. set CMD_LINE_ARGS=
  288. :setArgs
  289. if ""%1""=="""" goto doneSetArgs
  290. set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
  291. shift
  292. goto setArgs
  293. :doneSetArgs
  294. rem Execute Java with the applicable properties
  295. if not "%JPDA%" == "" goto doJpda
  296. if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
  297. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  298. goto end
  299. :doSecurity
  300. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  301. goto end
  302. :doJpda
  303. if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
  304. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  305. goto end
  306. :doSecurityJpda
  307. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  308. goto end
  309. :end

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注