揭开tomcat神秘的面纱之启动脚本
2018-11-04 本文已影响0人
一滴水的坚持
在启动tomcat的过程中,在linux下,我们会使用bin/startup.sh
脚本,在windows下开发的过程中一般会使用bin/start.bat
脚本。
本菜鸟在mac下启动如下:
jetty@zhangbodeMBP:~/Documents/software/apache-tomcat-7.0.76/bin$ ./startup.sh
Using CATALINA_BASE: /Users/jetty/Documents/software/apache-tomcat-7.0.76
Using CATALINA_HOME: /Users/jetty/Documents/software/apache-tomcat-7.0.76
Using CATALINA_TMPDIR: /Users/jetty/Documents/software/apache-tomcat-7.0.76/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Using CLASSPATH: /Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
Tomcat started.
启动之后,看进程
jetty@zhangbodeMBP:~/Documents/software/apache-tomcat-7.0.76/bin$ ps -ef |grep java
501 24718 1 0 2:58PM ttys001 0:20.08
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java
-Djava.util.logging.config.file=/Users/jetty/Documents/software/apache-tomcat-7.0.76/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djdk.tls.ephemeralDHKeySize=2048
-Djava.endorsed.dirs=/Users/jetty/Documents/software/apache-tomcat-7.0.76/endorsed
-classpath /Users/jetty/Documents/software/apache-tomcat7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
-Dcatalina.base=/Users/jetty/Documents/software/apache-tomcat-7.0.76
-Dcatalina.home=/Users/jetty/Documents/software/apache-tomcat-7.0.76
-Djava.io.tmpdir=/Users/jetty/Documents/software/apache-tomcat-7.0.76/temp
org.apache.catalina.startup.Bootstrap start
简单来看,startup.sh
脚本就是执行了一个java org.apache.catalina.startup.Bootstrap start
命令,简单来说,和我们跑一个main
方法没什么区别。
最后,看一下startup.sh
脚本究竟是些啥?
揭开startup.sh
的头盖骨
#!/bin/sh
os400=false
case "`uname`" in
OS400*) os400=true;;
esac
PRG="$0" #读取脚本名
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
#上面循环语句的意思是保证文件路径不是一个连接,使用循环直至找到文件原地址
PRGDIR=`dirname "$PRG"` #获取这个脚本的目录
EXECUTABLE=catalina.sh #可执行脚本实质是catalina.sh
if $os400; then
eval
else
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
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
#判断脚本catalina.sh是否存在并有可执行权限,没有执行权限就退出
exec "$PRGDIR"/"$EXECUTABLE" start "$@"
将脚本加了一些注释,看起来更加清楚:
os400=false
case "`uname`" in
OS400*) os400=true;;
esac
PRG="$0"
echo "PRG=$PRG"
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
PRGDIR=`dirname "$PRG"`
echo "可执行脚本的最终文件路径位置为:PRGDIR=$PRGDIR"
EXECUTABLE=catalina.sh
if $os400; then
eval
else
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
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
echo "最终执行的脚本为$PRGDIR/$EXECUTABLE start $@"
exec "$PRGDIR"/"$EXECUTABLE" start "$@"
执行结果为:
jetty@zhangbodeMBP:~/Documents/software/apache-tomcat-7.0.76/bin$ ./startup.sh
PRG=./startup.sh
可执行脚本的最终文件路径位置为:PRGDIR=.
最终执行的脚本为./catalina.sh start
Using CATALINA_BASE: /Users/jetty/Documents/software/apache-tomcat-7.0.76
Using CATALINA_HOME: /Users/jetty/Documents/software/apache-tomcat-7.0.76
Using CATALINA_TMPDIR: /Users/jetty/Documents/software/apache-tomcat-7.0.76/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Using CLASSPATH: /Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
Tomcat started.
jetty@zhangbodeMBP:~/Documents/software/apache-tomcat-7.0.76/bin$
可以看到,最终执行的脚本为catalina.sh
继续揭开catalina.sh
的头盖骨
本菜鸟在catalina.sh脚本中,添加了一些日志,看起来更加方便
echo "catalina.sh is handling"
cygwin=false #windows平台上运行的类UNIX模拟环境
darwin=false #macOS
os400=false #小型机
hpux=false
case "`uname`" in
CYGWIN*) cygwin=true;;
Darwin*) darwin=true;;
OS400*) os400=true;;
HP-UX*) hpux=true;;
esac
PRG="$0" #此处的RPG抓取的是文件名,因为可能是符号链接,所以下面循环语句的作用就是找到文件真实源路径
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
PRGDIR=`dirname "$PRG"`
echo "catalina.sh handling 获取执行脚本全路径:PRGDIR=$PRGDIR"
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
#上面两个语句判断变量$CATALINA_HOME和$CATALINA_BASE是否存在,不存在则给予附值
echo "catalina.sh handling 获取执行脚本全路径:CATALINA_HOME=$CATALINA_HOME"
echo "catalina.sh handling 获取执行脚本全路径:CATALINA_BASE=$CATALINA_BASE"
CLASSPATH=
# 设置用来初始化SystemClassLoader的CLASSPATH变量:通过执行脚本setclasspath.sh完成
# 注意:脚本setclasspath.sh改变了原来的$CLASSPATH变量,而把它设为:
# CLASSPATH=$CATALINA_HOME/bin/bootstrap.jar:$JAVA_HOME/lib/tools.jar
# 其中bootstrap.jar是package org.apache.catalina.startup的打包文件,
# 含有启动方法org.apache.catalina.startup.Bootstrap.main(String[] args)
# tools.jar含有javac编译器,用来把jsp文件编译成为servlet class
if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
. "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
. "$CATALINA_HOME/bin/setenv.sh"
fi
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
[ -n "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"`
[ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"`
[ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"`
[ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For OS400
if $os400; then
COMMAND='chgjob job('$JOBNAME') runpty(6)'
system $COMMAND
# Enable multi threading
export QIBM_MULTI_THREADED=Y
fi
if $os400; then
. "$CATALINA_HOME"/bin/setclasspath.sh
else
if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then
. "$CATALINA_HOME"/bin/setclasspath.sh
else
echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"
echo "This file is needed to run this program"
exit 1
fi
fi
#将变量赋值
if [ ! -z "$CLASSPATH" ] ; then
CLASSPATH="$CLASSPATH":
fi
CLASSPATH="$CLASSPATH""$CATALINA_HOME"/bin/bootstrap.jar
#指定classPath路径
#指定日志输出路径
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi
echo "catalina.sh handling 获取执行脚本全路径:CATALINA_OUT=$CATALINA_OUT"
if [ -z "$CATALINA_TMPDIR" ] ; then
CATALINA_TMPDIR="$CATALINA_BASE"/temp
fi
#将tomcat-juli.jar加入到classPath路径上去
if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then
CLASSPATH=$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar
else
CLASSPATH=$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar
fi
echo "catalina.sh handling 获取执行脚本全路径:CLASSPATH=$CLASSPATH"
#获取当前shell运行的终端设备
have_tty=0
if [ "`tty`" != "not a tty" ]; then
have_tty=1
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
JRE_HOME=`cygpath --absolute --windows "$JRE_HOME"`
CATALINA_HOME=`cygpath --absolute --windows "$CATALINA_HOME"`
CATALINA_BASE=`cygpath --absolute --windows "$CATALINA_BASE"`
CATALINA_TMPDIR=`cygpath --absolute --windows "$CATALINA_TMPDIR"`
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
fi
if [ -z "$JSSE_OPTS" ] ; then
JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
fi
JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS"
echo "catalina.sh handling 获取执行脚本全路径:JAVA_OPTS=$JAVA_OPTS"
#确认$LOGGING_MANAGER变量
if [ -z "$LOGGING_CONFIG" ]; then
if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then
LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
else
# Bugzilla 45585
LOGGING_CONFIG="-Dnop"
fi
fi
if [ -z "$LOGGING_MANAGER" ]; then
LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
fi
echo "catalina.sh handling 获取执行脚本全路径:LOGGING_CONFIG=$LOGGING_CONFIG"
if [ -z "$USE_NOHUP" ]; then
if $hpux; then
USE_NOHUP="true"
else
USE_NOHUP="false"
fi
fi
unset _NOHUP
if [ "$USE_NOHUP" = "true" ]; then
_NOHUP=nohup
fi
if [ $have_tty -eq 1 ]; then
echo "Using CATALINA_BASE: $CATALINA_BASE"
echo "Using CATALINA_HOME: $CATALINA_HOME"
echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"
if [ "$1" = "debug" ] ; then
echo "Using JAVA_HOME: $JAVA_HOME"
else
echo "Using JRE_HOME: $JRE_HOME"
fi
echo "Using CLASSPATH: $CLASSPATH"
if [ ! -z "$CATALINA_PID" ]; then
echo "Using CATALINA_PID: $CATALINA_PID"
fi
fi
#获取第一次参数,jpda在后面的说明为:jpda start JPDA调试模式
if [ "$1" = "jpda" ] ; then
if [ -z "$JPDA_TRANSPORT" ]; then
JPDA_TRANSPORT="dt_socket"
fi
if [ -z "$JPDA_ADDRESS" ]; then
JPDA_ADDRESS="8000" #设置调试端口
fi
if [ -z "$JPDA_SUSPEND" ]; then
JPDA_SUSPEND="n"
fi
if [ -z "$JPDA_OPTS" ]; then #调试参数设置
JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
fi
CATALINA_OPTS="$JPDA_OPTS $CATALINA_OPTS"
shift
fi
if [ "$1" = "debug" ] ; then #单步调试
if $os400; then
echo "Debug command not available on OS400"
exit 1
else
shift
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi
shift
exec "$_RUNJDB" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
-sourcepath "$CATALINA_HOME"/../../java \
-Djava.security.manager \
-Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
org.apache.catalina.startup.Bootstrap "$@" start
else
exec "$_RUNJDB" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
-sourcepath "$CATALINA_HOME"/../../java \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
org.apache.catalina.startup.Bootstrap "$@" start
fi
fi
elif [ "$1" = "run" ]; then #run命令处理
shift
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi
shift
eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Djava.security.manager \
-Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start
else
eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start
fi
elif [ "$1" = "start" ] ; then #start命令 也就是启动命令
if [ ! -z "$CATALINA_PID" ]; then
if [ -f "$CATALINA_PID" ]; then
if [ -s "$CATALINA_PID" ]; then
echo "Existing PID file found during start."
if [ -r "$CATALINA_PID" ]; then
PID=`cat "$CATALINA_PID"`
ps -p $PID >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "Tomcat appears to still be running with PID $PID. Start aborted."
echo "If the following process is not a Tomcat process, remove the PID file and try again:"
ps -f -p $PID
exit 1
else
echo "Removing/clearing stale PID file."
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$CATALINA_PID" ]; then
cat /dev/null > "$CATALINA_PID"
else
echo "Unable to remove or clear stale PID file. Start aborted."
exit 1
fi
fi
fi
else
echo "Unable to read PID file. Start aborted."
exit 1
fi
else
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ ! -w "$CATALINA_PID" ]; then
echo "Unable to remove or write to empty PID file. Start aborted."
exit 1
fi
fi
fi
fi
fi
shift
touch "$CATALINA_OUT" #创建日志文件
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi
shift
eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Djava.security.manager \
-Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
echo "catalina.sh handling 获取执行脚本:"
echo "$_NOHUP $_RUNJAVA $LOGGING_CONFIG $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS -Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS -classpath $CLASSPATH -Djava.security.manager -Djava.security.policy==\"$CATALINA_BASE/conf/catalina.policy\"“
echo "-Dcatalina.base="\"$CATALINA_BASE\" -Dcatalina.home="\"$CATALINA_HOME\" -Djava.io.tmpdir="\"$CATALINA_TMPDIR\" org.apache.catalina.startup.Bootstrap "$@" start >> \"$CATALINA_OUT\" 2>&1 \"&\""
else
eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
echo "catalina.sh handling 获取执行脚本:"
echo "$_NOHUP $_RUNJAVA $LOGGING_CONFIG $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS -Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS -classpath $CLASSPATH -Dcatalina.base=$CATALINA_BASE -Dcatalina.home=$CATALINA_HOME -Djava.io.tmpdir=$CATALINA_TMPDIR org.apache.catalina.startup.Bootstrap start >> $CATALINA_OUT 2>&1 &"
fi
if [ ! -z "$CATALINA_PID" ]; then
echo $! > "$CATALINA_PID"
fi
echo "Tomcat started."
#如果是stop处理
elif [ "$1" = "stop" ] ; then
shift
SLEEP=5
if [ ! -z "$1" ]; then
echo $1 | grep "[^0-9]" >/dev/null 2>&1
if [ $? -gt 0 ]; then
SLEEP=$1
shift
fi
fi
FORCE=0
if [ "$1" = "-force" ]; then
shift
FORCE=1
fi
if [ ! -z "$CATALINA_PID" ]; then
if [ -f "$CATALINA_PID" ]; then
if [ -s "$CATALINA_PID" ]; then
kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
if [ $? -gt 0 ]; then
echo "PID file found but no matching process was found. Stop aborted."
exit 1
fi
else
echo "PID file is empty and has been ignored."
fi
else
echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted."
exit 1
fi
fi
eval "\"$_RUNJAVA\"" $LOGGING_MANAGER $JAVA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" stop
# stop failed. Shutdown port disabled? Try a normal kill.
if [ $? != 0 ]; then
if [ ! -z "$CATALINA_PID" ]; then
echo "The stop command failed. Attempting to signal the process to stop through OS signal."
kill -15 `cat "$CATALINA_PID"` >/dev/null 2>&1
fi
fi
if [ ! -z "$CATALINA_PID" ]; then
if [ -f "$CATALINA_PID" ]; then
while [ $SLEEP -ge 0 ]; do
kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
if [ $? -gt 0 ]; then
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$CATALINA_PID" ]; then
cat /dev/null > "$CATALINA_PID"
# If Tomcat has stopped don't try and force a stop with an empty PID file
FORCE=0
else
echo "The PID file could not be removed or cleared."
fi
fi
echo "Tomcat stopped."
break
fi
if [ $SLEEP -gt 0 ]; then
sleep 1
fi
if [ $SLEEP -eq 0 ]; then
echo "Tomcat did not stop in time."
if [ $FORCE -eq 0 ]; then
echo "PID file was not removed."
fi
echo "To aid diagnostics a thread dump has been written to standard out."
kill -3 `cat "$CATALINA_PID"`
fi
SLEEP=`expr $SLEEP - 1 `
done
fi
fi
KILL_SLEEP_INTERVAL=5
if [ $FORCE -eq 1 ]; then
if [ -z "$CATALINA_PID" ]; then
echo "Kill failed: \$CATALINA_PID not set"
else
if [ -f "$CATALINA_PID" ]; then
PID=`cat "$CATALINA_PID"`
echo "Killing Tomcat with the PID: $PID"
kill -9 $PID
while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do
kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
if [ $? -gt 0 ]; then
rm -f "$CATALINA_PID" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$CATALINA_PID" ]; then
cat /dev/null > "$CATALINA_PID"
else
echo "The PID file could not be removed."
fi
fi
echo "The Tomcat process has been killed."
break
fi
if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
sleep 1
fi
KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `
done
if [ $KILL_SLEEP_INTERVAL -lt 0 ]; then
echo "Tomcat has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
fi
fi
fi
fi
elif [ "$1" = "configtest" ] ; then
eval "\"$_RUNJAVA\"" $LOGGING_MANAGER $JAVA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap configtest
result=$?
if [ $result -ne 0 ]; then
echo "Configuration error detected!"
fi
exit $result
elif [ "$1" = "version" ] ; then
"$_RUNJAVA" \
-classpath "$CATALINA_HOME/lib/catalina.jar" \
org.apache.catalina.util.ServerInfo
else
echo "Usage: catalina.sh ( commands ... )"
echo "commands:"
if $os400; then
echo " debug Start Catalina in a debugger (not available on OS400)"
echo " debug -security Debug Catalina with a security manager (not available on OS400)"
else
echo " debug Start Catalina in a debugger"
echo " debug -security Debug Catalina with a security manager"
fi
echo " jpda start Start Catalina under JPDA debugger"
echo " run Start Catalina in the current window"
echo " run -security Start in the current window with security manager"
echo " start Start Catalina in a separate window"
echo " start -security Start in a separate window with security manager"
echo " stop Stop Catalina, waiting up to 5 seconds for the process to end"
echo " stop n Stop Catalina, waiting up to n seconds for the process to end"
echo " stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running"
echo " stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running"
echo " configtest Run a basic syntax check on server.xml - check exit code for result"
echo " version What version of tomcat are you running?"
echo "Note: Waiting for the process to end and use of the -force option require that \$CATALINA_PID is defined"
exit 1
fi
执行结果:
jetty@zhangbodeMBP:~/Documents/software/apache-tomcat-7.0.76/bin$ ./startup.sh
PRG=./startup.sh
可执行脚本的最终文件路径位置为:PRGDIR=.
最终执行的脚本为./catalina.sh start
catalina.sh is handling
catalina.sh handling 获取执行脚本全路径:PRGDIR=/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin
catalina.sh handling 获取执行脚本全路径:CATALINA_HOME=/Users/jetty/Documents/software/apache-tomcat-7.0.76
catalina.sh handling 获取执行脚本全路径:CATALINA_BASE=/Users/jetty/Documents/software/apache-tomcat-7.0.76
catalina.sh handling 获取执行脚本全路径:CATALINA_OUT=/Users/jetty/Documents/software/apache-tomcat-7.0.76/logs/catalina.out
catalina.sh handling 获取执行脚本全路径:CLASSPATH=/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
catalina.sh handling 获取执行脚本全路径:JAVA_OPTS= -Djdk.tls.ephemeralDHKeySize=2048
catalina.sh handling 获取执行脚本全路径:LOGGING_CONFIG=-Djava.util.logging.config.file=/Users/jetty/Documents/software/apache-tomcat-7.0.76/conf/logging.properties
Using CATALINA_BASE: /Users/jetty/Documents/software/apache-tomcat-7.0.76
Using CATALINA_HOME: /Users/jetty/Documents/software/apache-tomcat-7.0.76
Using CATALINA_TMPDIR: /Users/jetty/Documents/software/apache-tomcat-7.0.76/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Using CLASSPATH: /Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
catalina.sh handling 获取执行脚本:
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java -Djava.util.logging.config.file=/Users/jetty/Documents/software/apache-tomcat-7.0.76/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=""/Users/jetty/Documents/software/apache-tomcat-7.0.76/endorsed"" -classpath ""/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar"" -Dcatalina.base=""/Users/jetty/Documents/software/apache-tomcat-7.0.76"" -Dcatalina.home=""/Users/jetty/Documents/software/apache-tomcat-7.0.76"" -Djava.io.tmpdir=""/Users/jetty/Documents/software/apache-tomcat-7.0.76/temp"" org.apache.catalina.startup.Bootstrap "" start >> "/Users/jetty/Documents/software/apache-tomcat-7.0.76/logs/catalina.out" 2>&1 &"
Tomcat started.
可以看到,在catalina.sh
中,组装各种参数,最后执行java org.apache.catalina.startup.Bootstrap start
命令
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java
-Djava.util.logging.config.file=/Users/jetty/Documents/software/apache-tomcat-7.0.76/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djdk.tls.ephemeralDHKeySize=2048
-Djava.endorsed.dirs=/Users/jetty/Documents/software/apache-tomcat-7.0.76/endorsed
-classpath /Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
-Dcatalina.base=/Users/jetty/Documents/software/apache-tomcat-7.0.76
-Dcatalina.home=/Users/jetty/Documents/software/apache-tomcat-7.0.76
-Djava.io.tmpdir=/Users/jetty/Documents/software/apache-tomcat-7.0.76/temp
org.apache.catalina.startup.Bootstrap start >> /Users/jetty/Documents/software/apache-tomcat-7.0.76/logs/catalina.out 2>&1 &
这就和控制台进程信息保持一致了。
jetty@zhangbodeMBP:~/Documents/software/apache-tomcat-7.0.76/bin$ ps -ef |grep java
501 24718 1 0 2:58PM ttys001 0:20.08
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java
-Djava.util.logging.config.file=/Users/jetty/Documents/software/apache-tomcat-7.0.76/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djdk.tls.ephemeralDHKeySize=2048
-Djava.endorsed.dirs=/Users/jetty/Documents/software/apache-tomcat-7.0.76/endorsed
-classpath /Users/jetty/Documents/software/apache-tomcat7.0.76/bin/bootstrap.jar:/Users/jetty/Documents/software/apache-tomcat-7.0.76/bin/tomcat-juli.jar
-Dcatalina.base=/Users/jetty/Documents/software/apache-tomcat-7.0.76
-Dcatalina.home=/Users/jetty/Documents/software/apache-tomcat-7.0.76
-Djava.io.tmpdir=/Users/jetty/Documents/software/apache-tomcat-7.0.76/temp
org.apache.catalina.startup.Bootstrap start