[shell] tomcat启动脚本赏析 – 2. catalina.sh

#!/bin/sh



JPDA_ADDRESS=5566 
JPDA_SUSPEND=n


# Only set CATALINA_HOME if not already set
#这里利用了 && 的短路功能
#catalina.sh是在bin目录下,cd "$PRGDIR/.."的作用下是跳到bin目录的上级目录,这时再执行pwd即可得tomcat的目录
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`

# Copy CATALINA_BASE from CATALINA_HOME if not already set
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"


if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then #setenv.sh存在并且当前用户有可读权限
  . "$CATALINA_BASE/bin/setenv.sh"  #执行一下setenv.sh,点号在这里的作用是临时执行一下脚本但不改变脚本文件的执行权限
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
  . "$CATALINA_HOME/bin/setenv.sh"
fi



  if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then
    BASEDIR="$CATALINA_HOME"
    . "$CATALINA_HOME"/bin/setclasspath.sh #设置一下classpath, 这里面会定义$_RUNJAVA变量,供下面使用
  else
    echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"
    echo "This file is needed to run this program"
    exit 1
  fi






#下面设定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="$CATALINA_OPTS $JPDA_OPTS"
  shift  #意思是:为本脚本的提供jpda参数已经消耗掉了; 从现在开始 $1 != jpda
fi


if
 #.......
elif [ "$1" = "run" ]; then
  shift
   #正式启动, exec用于执行一个命令
    exec "$_RUNJAVA" "$LOGGING_CONFIG" $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

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.