Tomcat启动脚本catalina.sh

1 - 概述

脚本catalina.sh用于启动和关闭tomcat服务器,是最关键的脚本
另外的脚本startup.sh和shutdown.sh都是使用不同的参数调用了该脚本
该脚本的使用方法如下(引自该脚本本身):

  echo "Usage: catalina.sh ( commands ... )"   echo "commands:"   echo "  debug             Start Catalina in a debugger"   echo "  debug -security   Debug Catalina with a security manager"   echo "  embedded          Start Catalina in embedded mode"   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" 

 

2 - 脚本分析

#!/bin/sh # ============================================================= # 该脚本设置正确的环境变量和系统信息,然后启动或者停止tomcat server # 具体的操作是: # 使用命令行参数作为args的实参,调用org.apache.catalina.startup.Bootstrap.main(String[]) # ==============================================================  # ----------------------------------------------------------------------------- # Start/Stop Script for the CATALINA Server # # Environment Variable Prequisites # # CATALINA_HOME May point at your Catalina "build" directory. # # CATALINA_BASE (Optional) Base directory for resolving dynamic portions # of a Catalina installation. If not present, resolves to # the same directory that CATALINA_HOME points to. # # CATALINA_OPTS (Optional) Java runtime options used when the "start", # "stop", or "run" command is executed. # # CATALINA_TMPDIR (Optional) Directory path location of temporary directory # the JVM should use (java.io.tmpdir). Defaults to # $CATALINA_BASE/temp. # # JAVA_HOME Must point at your Java Development Kit installation. # # JAVA_OPTS (Optional) Java runtime options used when the "start", # "stop", or "run" command is executed. # # JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start" # command is executed. The default is "dt_socket". # # JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start" # command is executed. The default is 8000. # # JSSE_HOME (Optional) May point at your Java Secure Sockets Extension # (JSSE) installation, whose JAR files will be added to the # system class path used to start Tomcat. # # CATALINA_PID (Optional) Path of the file which should contains the pid # of catalina startup java process, when start (fork) is used # -----------------------------------------------------------------------------  # OS specific support. $var _must_ be set to either true or false.   cygwin=false case "`uname`" in CYGWIN*) cygwin=true;; esac # PRG是被执行的脚本的名称,可以认为PRG=="catalina.sh",也可能是某个符号链,指向该脚本。   PRG="$0" # 处理了一下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是PRG的目录路径名称  PRGDIR=`dirname "$PRG"` # 执行$CATALINA_HOME/BIN/setenv.sh来设置环境变量 # [ -r filename ] 是判断是否文件存在且可读  CATALINA_HOME=`cd "$PRGDIR/.." ; pwd` if [ -r "$CATALINA_HOME"/bin/setenv.sh ]; then   . "$CATALINA_HOME"/bin/setenv.sh fi # For Cygwin, 确保所有的路径名都符合UNIX格式  if $cygwin; then   [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_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"`   [ -n "$JSSE_HOME" ] && JSSE_HOME=`cygpath --path --unix "$JSSE_HOME"` fi # 设置用来初始化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_HOME"/bin/setclasspath.sh ]; then   BASEDIR="$CATALINA_HOME"   . "$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 # 在CLASSPATH环境变量后面补上一些JAR文件 # [ -n string ] 判断字符串是否长度非0;[ -z string ] 判断字符串是否长度为0 # 它们可以用来判断是否某个环境变量已经被设置   if [ -n "$JSSE_HOME" ]; then   CLASSPATH="$CLASSPATH":"$JSSE_HOME"/lib/jcert.jar:"$JSSE_HOME"/lib/jnet.jar:"$JSSE_HOME"/lib/jsse.jar fi CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/bootstrap.jar # 指定CATALINA_BASE变量 # 如果CATALINA_BASE没有值,就让CATALINA_BASE=$CATALINA_HOME   if [ -z "$CATALINA_BASE" ] ; then   CATALINA_BASE="$CATALINA_HOME" fi # 指定CATALINA_TMPDIR变量 # 如果CATALINA_TMPDIR没有值,就让CATALINA_TMPDIR=$CATALINA_BASE/temp  if [ -z "$CATALINA_TMPDIR" ] ; then   # Define the java.io.tmpdir to use for Catalina   CATALINA_TMPDIR="$CATALINA_BASE"/temp fi # For Cygwin, 在执行java程序前把路径名转换成WINDOWS格式  if $cygwin; then   JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`   CATALINA_HOME=`cygpath --path --windows "$CATALINA_HOME"`   CATALINA_BASE=`cygpath --path --windows "$CATALINA_BASE"`   CATALINA_TMPDIR=`cygpath --path --windows "$CATALINA_TMPDIR"`   CLASSPATH=`cygpath --path --windows "$CLASSPATH"`   JSSE_HOME=`cygpath --path --windows "$JSSE_HOME"` fi # 显示信息,然后执行命令 echo "Using CATALINA_BASE: $CATALINA_BASE" echo "Using CATALINA_HOME: $CATALINA_HOME" echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR" echo "Using JAVA_HOME: $JAVA_HOME"  # ......当执行catalina.sh的参数是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_OPTS" ]; then     JPDA_OPTS="-Xdebug -Xrunjdwp:transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=n"   fi   CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"   shift fi # ......当执行catalina.sh的参数是debug的时候......  if [ "$1" = "debug" ] ; then   shift   if [ "$1" = "-security" ] ; then     echo "Using Security Manager"     shift     exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS /       -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" /       -sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-4.0/catalina/src/share /       -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" $JAVA_OPTS $CATALINA_OPTS /       -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" /       -sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-4.0/catalina/src/share /       -Dcatalina.base="$CATALINA_BASE" /       -Dcatalina.home="$CATALINA_HOME" /       -Djava.io.tmpdir="$CATALINA_TMPDIR" /       org.apache.catalina.startup.Bootstrap "$@" start   fi # ......当执行catalina.sh的参数是embedded的时候......  elif [ "$1" = "embedded" ] ; then   shift   echo "Embedded Classpath: $CLASSPATH"   exec "$_RUNJAVA" $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.Embedded "$@" # +----------------------------------------------------+ # | ......当执行catalina.sh的参数是run的时候...... | # | 在当前窗口中启动tomcat服务器!!! | # +----------------------------------------------------+  elif [ "$1" = "run" ]; then # 把参数run去掉    shift # 如果参数是run -security,则启动Security Manager    if [ "$1" = "-security" ] ; then     echo "Using Security Manager"     shift     exec "$_RUNJAVA" $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     # 如果参数是孤单的run,则在本窗口中启动tomcat服务器 # 在本窗口中启动的方法是使用exec,让当前进程fork一个新进程来启动tomcat,当前进程是tomcat的父进程 # 启动tomcat的类是org.apache.catalina.startup.Bootstrap.main("start");    else     exec "$_RUNJAVA" $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 # +----------------------------------------------------+ # | ......当执行catalina.sh的参数是start的时候...... | # | 在新窗口中启动tomcat服务器!!! | # +----------------------------------------------------+  elif [ "$1" = "start" ] ; then # 把参数start去掉   shift # 创建一个文件(如果文件不存在的话)$CATALINA_BASE/logs/catalina.out     touch "$CATALINA_BASE"/logs/catalina.out # 如果参数是start -security,则启动Security Manager    if [ "$1" = "-security" ] ; then     echo "Using Security Manager"     shift     "$_RUNJAVA" $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_BASE"/logs/catalina.out 2>&1 &        if [ ! -z "$CATALINA_PID" ]; then         echo $! > $CATALINA_PID       fi # 如果参数是孤单的start,那么在新窗口中启动tomcat    else     "$_RUNJAVA" $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_BASE"/logs/catalina.out 2>&1 &        if [ ! -z "$CATALINA_PID" ]; then         echo $! > $CATALINA_PID       fi    fi # +----------------------------------------------------+ # | ......当执行catalina.sh的参数是stop的时候...... | # | 关闭tomcat服务器!!! | # +----------------------------------------------------+  elif [ "$1" = "stop" ] ; then # 把参数stop去掉    shift # 关闭tomcat服务器的类是org.apache.catalina.startup.Bootstrap->main("stop"); # 注意:java -D 是设置系统属性    exec "$_RUNJAVA" $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 "$@" stop # +----------------------------------------------------+ # | ......当执行catalina.sh的参数不可辨认的时候...... | # | 打印帮助信息,由此查看各种参数代表的意义!!! | # +----------------------------------------------------+  else   echo "Usage: catalina.sh ( commands ... )"   echo "commands:"   echo "  debug             Start Catalina in a debugger"   echo "  debug -security   Debug Catalina with a security manager"   echo "  embedded          Start Catalina in embedded mode"   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"   exit 1 fi 
Tomcat中设置远程调试设置 
http://blog.chinaunix.net/u/21344/showart_498246.html
 在tomcat/bin/的catalina.bat文件中加入下面的设置,当然SET CATALINA_OPTS 变量应该在使用它之前。这是在tomcat启动时设置一些虚拟机参数,使服务器允许远程连接功能,address=5888表示远程连接的端口号,可以设置成任意其他不冲突端口。其他的应用服务器比如weblogic也应该可以设置这些参数,有兴趣的可以测试。

SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5888

这样就Ok了,重启动tomcat,可以直接独立启动,而不用在eclipes的插件中启动。打开eclipse中的debug设置窗口,选择Remote Java Application ,新建一个debug项,输入服务器IP和刚才设置端口号,点ok就可以进入debug状态了。
不过要注意在Linux下,有一点点差异,是要编辑catalina.sh文件。

     而且要改成这样:

     CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5888"

     也就是把SET去掉,后面加双引号引起来,因为在Linux下,是没有SET这个语法的。

你可能感兴趣的:(Tomcat启动脚本catalina.sh)