@cyysu
2017-10-12T10:19:14.000000Z
字数 11685
阅读 932
- 时间:2017年10月12日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
- 版本:4.0
- 描述:批处理实战基础篇四,主要讲解tomcat下启动脚本,本文主要讲解catalina.bat这个脚本
Bat系列教程
其目录下面的脚本文件如下,有些重复的内容,这里不做过多介绍,有遇到什么不会的内容可以查看Bat系列教程
下面我们一一来分析其里面的内容,为了避免文本过长,每个文件单独一篇文章讲解。
# catalina.bat:: 我们重点讲解bat语法以及模仿别人如何编写,从他人代码中丰富自己,不对脚本中的逻辑关系进行过多介绍。@echo offrem Licensed to the Apache Software Foundation (ASF) under one or morerem contributor license agreements. See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.rem The ASF licenses this file to You under the Apache License, Version 2.0rem (the "License"); you may not use this file except in compliance withrem the License. You may obtain a copy of the License atremrem http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem 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 andrem limitations under the License.rem ---------------------------------------------------------------------------rem Start/Stop Script for the CATALINA Serverremrem Environment Variable Prerequisitesremrem Do not set the variables in this script. Instead put them into a scriptrem setenv.bat in CATALINA_BASE/bin to keep your customizations separate.remrem WHEN RUNNING TOMCAT AS A WINDOWS SERVICE:rem Note that the environment variables that affect the behavior of thisrem script will have no effect at all on Windows Services. As such, anyrem local customizations made in a CATALINA_BASE/bin/setenv.bat scriptrem will also have no effect on Tomcat when launched as a Windows Service.rem The configuration that controls Windows Services is stored in the Windowsrem Registry, and is most conveniently maintained using the "tomcatXw.exe"rem maintenance utility, where "X" is the major version of Tomcat you arerem running.remrem CATALINA_HOME May point at your Catalina "build" directory.remrem CATALINA_BASE (Optional) Base directory for resolving dynamic portionsrem of a Catalina installation. If not present, resolves torem the same directory that CATALINA_HOME points to.remrem CATALINA_OPTS (Optional) Java runtime options used when the "start",rem "run" or "debug" command is executed.rem Include here and not in JAVA_OPTS all options, that shouldrem only be used by Tomcat itself, not by the stop process,rem the version command etc.rem Examples are heap size, GC logging, JMX ports etc.remrem CATALINA_TMPDIR (Optional) Directory path location of temporary directoryrem the JVM should use (java.io.tmpdir). Defaults torem %CATALINA_BASE%\temp.remrem JAVA_HOME Must point at your Java Development Kit installation.rem Required to run the with the "debug" argument.remrem JRE_HOME Must point at your Java Runtime installation.rem Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOMErem are both set, JRE_HOME is used.remrem JAVA_OPTS (Optional) Java runtime options used when any commandrem is executed.rem Include here and not in CATALINA_OPTS all options, thatrem should be used by Tomcat and also by the stop process,rem the version command etc.rem Most options should go into CATALINA_OPTS.remrem JAVA_ENDORSED_DIRS (Optional) Lists of of semi-colon separated directoriesrem containing some jars in order to allow replacement of APIsrem created outside of the JCP (i.e. DOM and SAX from W3C).rem It can also be used to update the XML parser implementation.rem Defaults to $CATALINA_HOME/endorsed.remrem JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"rem command is executed. The default is "dt_socket".remrem JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start"rem command is executed. The default is 8000.remrem JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start"rem command is executed. Specifies whether JVM should suspendrem execution immediately after startup. Default is "n".remrem JPDA_OPTS (Optional) Java runtime options used when the "jpda start"rem command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,rem and JPDA_SUSPEND are ignored. Thus, all required jpdarem options MUST be specified. The default is:remrem -agentlib:jdwp=transport=%JPDA_TRANSPORT%,rem address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%remrem JSSE_OPTS (Optional) Java runtime options used to control the TLSrem implementation when JSSE is used. Default is:rem "-Djdk.tls.ephemeralDHKeySize=2048"remrem LOGGING_CONFIG (Optional) Override Tomcat's logging config filerem Example (all one line)rem set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"remrem LOGGING_MANAGER (Optional) Override Tomcat's logging managerrem Example (all one line)rem set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"remrem TITLE (Optional) Specify the title of Tomcat window. The defaultrem TITLE is Tomcat if it's not specified.rem Example (all one line)rem set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%]rem ---------------------------------------------------------------------------setlocal:: 第一段rem Suppress Terminate batch job on CTRL+Cif not ""%1"" == ""run"" goto mainEntryif "%TEMP%" == "" goto mainEntryif exist "%TEMP%\%~nx0.run" goto mainEntry:: %~nx0表示文件的名字 不带路径echo Y>"%TEMP%\%~nx0.run":: 如果%TEMP%\catalina.bat.run 不存在 那么就跳转mainEntryif not exist "%TEMP%\%~nx0.run" goto mainEntryecho Y>"%TEMP%\%~nx0.Y":: %~f0表示运行的文件全路径 %*代表所有参数call "%~f0" %* <"%TEMP%\%~nx0.Y"rem Use provided errorlevelset RETVAL=%ERRORLEVEL%del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1exit /B %RETVAL%:mainEntrydel /Q "%TEMP%\%~nx0.run" >NUL 2>&1:: 第二段rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not "%CATALINA_HOME%" == "" goto gotHomeset "CATALINA_HOME=%CURRENT_DIR%"if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomecd ..set "CATALINA_HOME=%cd%"cd "%CURRENT_DIR%":gotHomeif exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomeecho The CATALINA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programgoto end:okHomerem Copy CATALINA_BASE from CATALINA_HOME if not definedif not "%CATALINA_BASE%" == "" goto gotBaseset "CATALINA_BASE=%CATALINA_HOME%":gotBaserem Ensure that any user defined CLASSPATH variables are not used on startup,rem but allow them to be specified in setenv.bat, in rare case when it is needed.set CLASSPATH=rem Get standard environment variablesif not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHomecall "%CATALINA_BASE%\bin\setenv.bat"goto setenvDone:checkSetenvHomeif exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat":setenvDonerem Get standard Java environment variablesif exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspathecho Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"echo This file is needed to run this programgoto end:okSetclasspathcall "%CATALINA_HOME%\bin\setclasspath.bat" %1:: 这个格式是if的特殊用法 if 检测执行是否正确,同时if还可以检测变量是否定义 采用 if definedif errorlevel 1 goto endrem Add on extra jar file to CLASSPATHrem Note that there are no quotes as we do not want to introduce randomrem quotes into the CLASSPATHif "%CLASSPATH%" == "" goto emptyClasspathset "CLASSPATH=%CLASSPATH%;":emptyClasspathset "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"if not "%CATALINA_TMPDIR%" == "" goto gotTmpdirset "CATALINA_TMPDIR=%CATALINA_BASE%\temp":gotTmpdirrem Add tomcat-juli.jar to classpathrem tomcat-juli.jar can be over-ridden per instanceif not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHomeset "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"goto juliClasspathDone:juliClasspathHomeset "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar":juliClasspathDoneif not "%JSSE_OPTS%" == "" goto gotJsseOptsset JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048":gotJsseOptsset "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"if not "%LOGGING_CONFIG%" == "" goto noJuliConfigset LOGGING_CONFIG=-Dnopif not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfigset LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties":noJuliConfigif not "%LOGGING_MANAGER%" == "" goto noJuliManagerset LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager:noJuliManagerrem ----- Execute The Requested Command ---------------------------------------echo Using CATALINA_BASE: "%CATALINA_BASE%"echo Using CATALINA_HOME: "%CATALINA_HOME%"echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"if ""%1"" == ""debug"" goto use_jdkecho Using JRE_HOME: "%JRE_HOME%"goto java_dir_displayed:use_jdkecho Using JAVA_HOME: "%JAVA_HOME%":java_dir_displayedecho Using CLASSPATH: "%CLASSPATH%"set _EXECJAVA=%_RUNJAVA%set MAINCLASS=org.apache.catalina.startup.Bootstrapset ACTION=startset SECURITY_POLICY_FILE=set DEBUG_OPTS=set JPDA=if not ""%1"" == ""jpda"" goto noJpdaset JPDA=jpdaif not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransportset JPDA_TRANSPORT=dt_socket:gotJpdaTransportif not "%JPDA_ADDRESS%" == "" goto gotJpdaAddressset JPDA_ADDRESS=8000:gotJpdaAddressif not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspendset JPDA_SUSPEND=n:gotJpdaSuspendif not "%JPDA_OPTS%" == "" goto gotJpdaOptsset JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%:gotJpdaOptsshift:noJpdaif ""%1"" == ""debug"" goto doDebugif ""%1"" == ""run"" goto doRunif ""%1"" == ""start"" goto doStartif ""%1"" == ""stop"" goto doStopif ""%1"" == ""configtest"" goto doConfigTestif ""%1"" == ""version"" goto doVersionecho Usage: catalina ( commands ... )echo commands:echo debug Start Catalina in a debuggerecho debug -security Debug Catalina with a security managerecho jpda start Start Catalina under JPDA debuggerecho run Start Catalina in the current windowecho run -security Start in the current window with security managerecho start Start Catalina in a separate windowecho start -security Start in a separate window with security managerecho stop Stop Catalinaecho configtest Run a basic syntax check on server.xmlecho version What version of tomcat are you running?goto end:doDebugshiftset _EXECJAVA=%_RUNJDB%set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doRunshiftif not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doStartshiftif "%TITLE%" == "" set TITLE=Tomcatset _EXECJAVA=start "%TITLE%" %_RUNJAVA%if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doStopshiftset ACTION=stopset CATALINA_OPTS=goto execCmd:doConfigTestshiftset ACTION=configtestset CATALINA_OPTS=goto execCmd:doVersion%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfogoto end:execCmdrem Get remaining unshifted command line arguments and save them in theset CMD_LINE_ARGS=:setArgsif ""%1""=="""" goto doneSetArgsset CMD_LINE_ARGS=%CMD_LINE_ARGS% %1shiftgoto setArgs:doneSetArgsrem Execute Java with the applicable propertiesif not "%JPDA%" == "" goto doJpdaif not "%SECURITY_POLICY_FILE%" == "" goto doSecurity%_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%goto end:doSecurity%_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%goto end:doJpdaif not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda%_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%goto end:doSecurityJpda%_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%goto end:end
支付宝 微信