Tomcat7源码分析学习系列之一-----tomcat的启动文件startup的注释

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat7源码分析学习系列之一-----tomcat的启动文件startup的注释相关的知识,希望对你有一定的参考价值。

1. Windows系统,tomcat启动文件startup.bat

@echo off       rem 关闭回显,不显示下面的命令;
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem 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 and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------

setlocal                                   rem setlocal命令将启动批处理文件中环境变量的本地化。本地化将持续到出现匹配的 endlocal 命令或者到达批处理文件结尾为止

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"                        rem 设置环境变量CURRENT_DIR为当前目录--G:\server\apache-tomcat-7.0.75-src\bin
if not "%CATALINA_HOME%" == "" goto gotHome              rem 如果设置了环境变量CATALINA_HOME,即CATALINA_HOME不为空,转到 :gotHome
set "CATALINA_HOME=%CURRENT_DIR%"                  rem 没有设置环境变量CATALINA_HOME,则设置CATALINA_HOME=CURRENT_DIR,即当前文件所在目录
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome       rem 如果"%CATALINA_HOME%\bin\catalina.bat"存在,则跳转到:okHome
cd ..                                   rem 如果"%CATALINA_HOME%\bin\catalina.bat"不存在,回到当前目录的上级目录
set "CATALINA_HOME=%cd%"                      rem 设置CATALINA_HOME为当前目录,即startup.bat文件所在目录的上级目录
cd "%CURRENT_DIR%"                          rem 进入到环境变量CURRENT_DIR指定的目录--G:\server\apache-tomcat-7.0.75-src\bin
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly  rem 如果"%CATALINA_HOME%\bin\catalina.bat"不存在,提示CATALINA_HOME没有正确定义,该环境变量是运行tomcat必须的
echo This environment variable is needed to run this program
goto end rem 跳转到 :end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"        rem 设置环境变量EXECUTABLE=执行程序%CATALINA_HOME%\bin\catalina.bat

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec                   rem 如果EXECUTABLE存在,跳转到:okExec
echo Cannot find "%EXECUTABLE%"                  rem 如果EXECUTABLE即%CATALINA_HOME%\bin\catalina.bat不存在,则提示不能找到环境变                      rem 量%CATALINA_HOME%\bin\catalina.bat,
echo This file is needed to run this program               rem 这个文件是运行tomcat必须的
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=                          rem 设置命令行参数  
:setArgs
rem %[1-9]表示参数,参数是指在运行批处理文件时在文件名后面,以空格(或者Tab)分隔的字符串。变量可以从%0到%9,%0表示批处理命令本身,其它参数字符串用%1到%9顺序表示。
if ""%1""=="""" goto doneSetArgs                      rem 如果没有参数跳转到:doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1               rem 设置CMD_LINE_ARGS=当前命令行参数 加上第一个参数(%1)
shift                                    rem 把第二个参数移动到第一位,即变量%2移到1%
goto setArgs                                rem 循环把所有参数都添加到CMD_LINE_ARGS
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%               rem 启动EXECUTABLE即%CATALINA_HOME%\bin\catalina.bat ,参数为CMD_LINE_ARGS

:end

 

2.Linux/unix系统,tomcat启动文件startup.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
# -----------------------------------------------------------------------------

# Better OS/400 detection: see Bugzilla 31132
os400=false                                    #os400 IBM的操作系统
case "`uname`" in                                #uname获取操作系统名称,如果获取到的操作系统名称,是以OS400开头,这说明是IBM操作系统
OS400*) os400=true;;
esac

# resolve links - $0 may be a softlink
PRG="$0"                                  #[[email protected] Desktop]$ echo PRG="$0" PRG=/bin/bash,脚本本身的名字:$0,

#变量赋值,变量后面紧跟赋值符号"=",不能加空格,tab键等

while [ -h "$PRG" ] ; do                            #判断变量$PRG是不是连接,是的话,循环直到找到文件地址
ls=`ls -ld "$PRG"`
link=`expr "$ls" : ‘.*-> \(.*\)$‘`
if expr "$link" : ‘/.*‘ > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
#上面循环语句的意思是保证文件路径不是一个连接,使用循环直至找到文件原地址
#遇到一时看不明白的shell,可以拆解后自己在linux反复运行验证,一点点拆解就会明白的,但是还是不是很明白
PRGDIR=`dirname "$PRG"`                              #$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
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 "[email protected]"                          #./catalina.sh start +输入参数

#exec命令在执行时会把当前的shell process关闭,然后换到后面的命令继续执行。
#exec命令可以很好的进行脚本之间过渡,并且结束掉前一个脚本这样不会对后面执行的脚本造成干扰。
#exec 命令:常用来替代当前 shell 并重新启动一个 shell,换句话说,并没有启动子 shell。使用这一命令时任何现有环境都将会被清除。exec 也只有在对文件描述符进行操作的时候,exec 不会覆盖你当前的 shell 环境。
#exec 可以用于脚本执行完启动需要启动另一个脚本时使用,但须考虑到环境变量是否被继承

3.总结

startup.bat和startup.sh脚本实际上并没有做什么具体工作,主要功能是在从tomcat目录下找到正确的catalina.bat/catalina.sh,并调用catalina.bat /catalina.sh start命令 +命令行中的参数。 

即startup.bat/startup.sh的主要作用是调用catalina.bat/catalina.sh start命令(即其与命令行中执行catalina.bat/cata.ina.sh start作用一样)。

 





























































































以上是关于Tomcat7源码分析学习系列之一-----tomcat的启动文件startup的注释的主要内容,如果未能解决你的问题,请参考以下文章

Oracle Ask Tom分区学习系列: 面向开发者的分区(Partitioning)教程

Oracle Ask Tom分区学习系列: 面向开发者的分区(Partitioning)教程

Oracle Ask Tom分区学习系列: 面向开发者的分区(Partitioning)教程

死磕Tomcat7源码之一:解析web.xml

死磕Tomcat7源码之二:web组件初始化

tomcat架构分析(概览)