Linux下Tomcat的安装部署

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chapterchou/article/details/62422289

Linux下Tomcat的安装部署
一、 linux下安装JDK1.6
JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品。自从Java推出以来,JDK已经成为使用最广泛的Java SDK。JDK 是整个Java的核心,包括了Java运行环境,Java工具和Java基础的类库。下面我们就来学习如何在CentOS系统下安装 JDK。
1. CentOS系统创建安装目录

mkdir /usr/java

  1. 下载(oracle官网下载,需要登录认证,无法直接下载,直接网站上下载上传)
    先去 www.oracle.com 网站寻找到 最新jdk1.6(JDK 6 Update 30)的下载地址:
    http://download.oracle.com/otn-pub/java/jdk/6u30-b12/jdk-6u30-linux-i586-rpm.bin
    在当前目录下(/usr/java)执行:
    wget http://download.oracle.com/otn-pub/java/jdk/6u30-b12/jdk-6u30-linux-i586-rpm.bin
    可以喝杯水,慢慢等待……
  2. 在CentOS系统中安装
    下载完成之后执行命令,即可拥有对 .bin文件的执行权限
    chmod 777 *.bin
    然后输入:
    ./*.bin
    安装jdk时会显示出JDK的安装许可协议,按空格翻页,最后程序会问你是不是同意上面的协议,当然同意啦,输入“yes”之后开始解压JDK到当前目录。此时屏幕上会显示解压的进度。
    解压完成后 /usr/java目录下会新建一个名为“jdk**”的目录,至此我们已经在CentOS系统下安装好了JDK。
    如果是*.i586.bin的话

chmod a+x *-i586.bin

./*-i586.bin

  1. 配置
    安装后之后需要配置环境变量。
    vi /etc/profile
    在最后加入以下几行:
    export JAVA_HOME=/usr/java/jdk1.6.0_30
    export CLASSPATH=.: JAVAHOME/jre/lib/rt.jar: JAVA_HOME/lib/dt.jar: JAVAHOME/lib/tools.jarexportPATH= PATH:$JAVA_HOME/bin
    注意,需要把jdk名字修改成您所下载的JDK当前版本。
    设置好JDK,输入 reboot 命令重启即可。
  2. 删除/卸载
    用 rm -rf jdk1.6.0_30 命令即可删除JDK,切记需要把配置文件的相关内容清空。
    这样就可以在CentOS系统下安装 JDK了。

二、 Linux下安装Tomcat
1.下载安装tomcat(http://tomcat.apache.org/
[root@master ~]# unzip apache-tomcat-6.0.30.zip
[root@master ~]# mv apache-tomcat-6.0.30/ /usr/local/
[root@master ~]cd /usr/local/
[root@master local]# ln -s /usr/local/apache-tomcat-6.0.30/ /usr/local/tomcat
[root@master local]# cd tomcat/bin/
[root@master bin]#ls
[root@master bin]#vim catalina.sh
添加以下内容:
CATALINA_HOME=/usr/local/apache-tomcat-6.0.30/
[root@master local]#chmod +x *.sh
2.启动tomcat服务器
[root@master tomcat]# /usr/local/tomcat /bin/catalina.sh start
Using CATALINA_BASE: /usr/local/apache-tomcat-6.0.30/
Using CATALINA_HOME: /usr/local/apache-tomcat-6.0.30/
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-6.0.30//temp
Using JRE_HOME: /usr/local/jdk1.6.0_05
Using CLASSPATH: /usr/local/apache-tomcat-6.0.30//bin/bootstrap.jar
[root@master logs]# cd /usr/local/tomcat/logs/
[root@master logs]# tail -f catalina.out
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Jul 4, 2011 11:06:57 AM org.apache.coyote.http11.Http11Protocol pause
INFO: Pausing Coyote HTTP/1.1 on http-8080
Jul 4, 2011 11:06:58 AM org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
Jul 4, 2011 11:06:58 AM org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8080
3.在浏览器中输入
http://localhost:8080/(如果不是本机,则输入对应的ip地址)
测试出现tomcat页面则测试成功
ps:需要说明的是tomcat的默认测试页面是放在webapps下面,这个其实是在server.xml文件中配置的,如下所示:

!/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/Stop Script for the CATALINA Server

#

Environment Variable Prerequisites

#

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_OUT (Optional) Full path to a file where stdout and stderr

will be redirected.

Default is $CATALINA_BASE/logs/catalina.out

#

CATALINA_OPTS (Optional) Java runtime options used when the “start”,

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.

Required to run the with the “debug” argument.

#

JRE_HOME Must point at your Java Development Kit installation.

Defaults to JAVA_HOME if empty.

#

JAVA_OPTS (Optional) Java runtime options used when the “start”,

“stop”, or “run” command is executed.

#

JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories

containing some jars in order to allow replacement of APIs

created outside of the JCP (i.e. DOM and SAX from W3C).

It can also be used to update the XML parser implementation.

Defaults to $CATALINA_HOME/endorsed.

#

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.

#

JPDA_SUSPEND (Optional) Java runtime options used when the “jpda start”

command is executed. Specifies whether JVM should suspend

execution immediately after startup. Default is “n”.

#

JPDA_OPTS (Optional) Java runtime options used when the “jpda start”

command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,

and JPDA_SUSPEND are ignored. Thus, all required jpda

options MUST be specified. The default is:

#

-agentlib:jdwp=transport=$JPDA_TRANSPORT,

address= JPDAADDRESS,server=y,suspend= JPDA_SUSPEND

#

CATALINA_PID (Optional) Path of the file which should contains the pid

of catalina startup java process, when start (fork) is used

#

LOGGING_CONFIG (Optional) Override Tomcat’s logging config file

Example (all one line)

LOGGING_CONFIG=”-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties”

#

LOGGING_MANAGER (Optional) Override Tomcat’s logging manager

Example (all one line)

LOGGING_MANAGER=”-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager”

#

Id:catalina.sh11460972011071315:25:05Zmarkt

—————————————————————————–

export JRE_HOME=/usr/java/jdk1.6.0_38
export CATALINA_HOME=/home/xrltest1/tomcat
JAVA_OPTS=”-server -Xms2048m -Xmx2048m -Xmn768m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:+UseParallelOldGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/xrltest1/tomcat/dumpfile/heap.bin -Xloggc:/home/xrltest1/tomcat/logs/gc.log”

作者添加的环境申明,如果一台服务器中有多台tomcat要使用不同版本的JVM,就可以直接这这边添加JRE_HOME,不需要再/etc/profile.d中再配置JRE_HOME环境变量

OS specific support. $var must be set to either true or false.

此处语句判断操作系统,同时对操作系统支持

os400是 IBM的AIX

darwin是MacOSX 操作环境的操作系统成份

Darwin是windows平台上运行的类UNIX模拟环境

cygwin=false
os400=false
darwin=false
case “uname” in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
Darwin*) darwin=true;;
esac

resolve links - $0 may be a softlink

此处的RPG抓取的是文件名,因为可能是符号链接,所以下面循环语句的作用就是找到文件真实源路径

PRG=” 0while[h PRG” ]; do
ls=ls -ld "$PRG"
link=expr "$ls" : '.*-> \(.*\)$'
if expr “ link:/.>/dev/null;thenPRG= link”
else
PRG=dirname "$PRG"/”$link”
fi
done

获取脚本目录真实目录地址

Get standard environment variables

PRGDIR=dirname "$PRG"

Only set CATALINA_HOME if not already set

[ -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”

上面两个语句判断变量 CATALINAHOME CATALINA_BASE是否存在,不存在则给予附值

CATALINA_BASE=”$CATALINA_HOME”

Ensure that any user defined CLASSPATH variables are not used on startup,

but allow them to be specified in setenv.sh, in rare case when it is needed.

CLASSPATH=

test –r File 文件存在并且可读

if [ -r “ CATALINABASE/bin/setenv.sh];then. CATALINA_BASE/bin/setenv.sh”
elif [ -r “ CATALINAHOME/bin/setenv.sh];then. CATALINA_HOME/bin/setenv.sh”
fi

默认的tomcat的bin目录下没有这个文件,可以自行编写setenv.sh这个文件设定环境变量

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  
  # Set job priority to standard for interactive (interactive - 6) by using  
  # the interactive priority - 6, the helper threads that respond to requests  
  # will be running at the same priority as interactive jobs.  
  COMMAND=’chgjob job(‘
JOBNAME’) runpty(6)’
system $COMMAND
# Enable multi threading
export QIBM_MULTI_THREADED=Y
fi

Get standard Java environment variables

if os400; then  
  # -r 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  
  BASEDIR=”
CATALINA_HOME”
. “CATALINA_HOME”/bin/setclasspath.sh  
else  
  #这点一定要注意了,本脚本中没有赋值却突然冒出的变量  
  #都是在setclasspath.sh这个脚本中执行赋值的。比如
_RUNJAVA
#作者会在下一篇文章中,分析setclasspath.sh这个脚本
if [ -r “ CATALINAHOME/bin/setclasspath.sh];thenBASEDIR= CATALINA_HOME”
. “ CATALINAHOME/bin/setclasspath.shelseechoCannotfind CATALINA_HOME/bin/setclasspath.sh”
echo “This file is needed to run this program”
exit 1
fi
fi

以上是出现在其他操作环境下的脚本的可用性设置,因默认linux系统,上面就不做解读了

if [ -z “ CATALINABASE];thenCATALINABASE= CATALINA_HOME”
fi

将变量赋值

Add tomcat-juli.jar and bootstrap.jar to classpath

tomcat-juli.jar can be over-ridden per instance

if [ ! -z “ CLASSPATH];thenCLASSPATH= CLASSPATH”:
fi

虽然前面已将做出CATALINA_BASE=”$CATALINA_HOME”的语句

此处还是添加了判断,增强了代码的健壮性

添加变量$CLASSPATH的值

if [ “ CATALINABASE"!=" CATALINA_HOME” ] && [ -r “ CATALINABASE/bin/tomcatjuli.jar];thenCLASSPATH= CLASSPATH"" CATALINABASE/bin/tomcatjuli.jar: CATALINA_HOME”/bin/bootstrap.jar
else
CLASSPATH=” CLASSPATH"" CATALINA_HOME”/bin/bootstrap.jar
fi

确认日志路径$CATALINA_OUT

这部分主要判断这些变量是否预定义,有就以预定义为主,没有就设置为默认格式

if [ -z “ CATALINAOUT];thenCATALINAOUT= CATALINA_BASE”/logs/catalina.out
fi
if [ -z “CATALINA_TMPDIR” ] ; then  
  # Define the java.io.tmpdir to use for Catalina  
  CATALINA_TMPDIR=”
CATALINA_BASE”/temp
fi

Bugzilla 37848: When no TTY is available, don’t output to console

获取当前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

Set juli LogManager config file if it is present and an override has not been issued

确认$LOGGING_CONFIG变量

if [ -z “ LOGGINGCONFIG];thenif[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

确认$LOGGING_MANAGER变量

if [ -z “ LOGGINGMANAGER];thenJAVAOPTS= JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager”
else
JAVA_OPTS=” JAVAOPTS LOGGING_MANAGER”
fi

—– Execute The Requested Command —————————————–

Bugzilla 37848: only output this if we have a TTY

正常情况下have_tty=1

if [ havettyeq1];thenechoUsingCATALINABASE: CATALINA_BASE”
echo “Using CATALINA_HOME: CATALINAHOMEechoUsingCATALINATMPDIR: CATALINA_TMPDIR”
if [ “ 1=debug];thenechoUsingJAVAHOME: JAVA_HOME”
else
echo “Using JRE_HOME: JREHOMEfiechoUsingCLASSPATH: CLASSPATH”
#以下这句判断设置的 CATALINAPID"UsingCATALINAPID: CATALINA_PID”,如果存在则不显示
if [ ! -z “ CATALINAPID];thenechoUsingCATALINAPID: CATALINA_PID”
fi
fi

以上就是在启动tomcat是输出的环境变量信息

#

获取第一次参数,jpda在后面的说明为:jpda start Start Catalina under JPDA debugger

if [ “ 1=jpda];thenif[z JPDA_TRANSPORT” ]; then
JPDA_TRANSPORT=”dt_socket”
fi
if [ -z “ JPDAADDRESS];thenJPDAADDRESS=8000fiif[z JPDA_SUSPEND” ]; then
JPDA_SUSPEND=”n”
fi
if [ -z “ JPDAOPTS];thenJPDAOPTS=agentlib:jdwp=transport= JPDA_TRANSPORT,address= JPDAADDRESS,server=y,suspend= JPDA_SUSPEND”
fi
CATALINA_OPTS=” CATALINAOPTS JPDA_OPTS”
shift
fi

JPDA 模式是开启远程debug模式,端口就是JPDA_ADDRESS,生产环境用不到,不去深入

这个是debug模式

值得注意的是变量$_RUNJDB,居然在全文没有找到赋值的语句。

从这边开始就注意了,里面的if-fi判断很多,一定要先调好格式再看

if [ “ 1=debug];thenif os400; then
echo “Debug command not available on OS400”
exit 1
else
shift
if [ “ 1=security];thenif[ have_tty -eq 1 ]; then
echo “Using Security Manager”
fi
shift
exec “ RUNJDB"" LOGGING_CONFIG” JAVAOPTS CATALINA_OPTS \
-Djava.endorsed.dirs=” JAVAENDORSEDDIRS"classpath" CLASSPATH” \
-sourcepath “ CATALINAHOME/../../java Djava.security.manager Djava.security.policy== CATALINA_BASE”/conf/catalina.policy \
-Dcatalina.base=” CATALINABASE Dcatalina.home= CATALINA_HOME” \
-Djava.io.tmpdir=” CATALINATMPDIR org.apache.catalina.startup.Bootstrap @” start
else
exec “ RUNJDB"" LOGGING_CONFIG” JAVAOPTS CATALINA_OPTS \
-Djava.endorsed.dirs=” JAVAENDORSEDDIRS"classpath" CLASSPATH” \
-sourcepath “ CATALINAHOME/../../java Dcatalina.base= CATALINA_BASE” \
-Dcatalina.home=” CATALINAHOME Djava.io.tmpdir= CATALINA_TMPDIR” \
org.apache.catalina.startup.Bootstrap “$@” start
fi
fi
#上面语句是适配AIX环境的,不做解读

当参数是debug时

elif [ “$1” = “run” ]; then
shift

当使用shift命令之后,原来的 2 1,并且原有的 1 #命令获得的参数个数也会少1

这样就可以使用$1获取第二个参数

if [ “ 1=security];thenif[ have_tty -eq 1 ]; then
echo “Using Security Manager”
fi
shift

作废掉前两个参数

exec “ RUNJAVA"" LOGGING_CONFIG” JAVAOPTS CATALINA_OPTS \
-Djava.endorsed.dirs=” JAVAENDORSEDDIRS"classpath" CLASSPATH” \
-Djava.security.manager \
-Djava.security.policy==” CATALINABASE/conf/catalina.policy Dcatalina.base= CATALINA_BASE” \
-Dcatalina.home=” CATALINAHOME Djava.io.tmpdir= CATALINA_TMPDIR” \
org.apache.catalina.startup.Bootstrap “ @startelseexec _RUNJAVA" " LOGGINGCONFIG JAVA_OPTS CATALINAOPTS Djava.endorsed.dirs= JAVA_ENDORSED_DIRS" -classpath " CLASSPATH Dcatalina.base= CATALINA_BASE” \
-Dcatalina.home=” CATALINAHOME Djava.io.tmpdir= CATALINA_TMPDIR” \
org.apache.catalina.startup.Bootstrap “$@” start

这个命令其实就是java命令,指定org.apache.catalina.startup.Bootstrap 这个class文件,$@和”start”作用类似于传参到前面的安格class里

fi

正式进入启动的过程啦~

elif [ “ 1=start];thenif[!z CATALINA_PID” ]; then
#test –z 字符串 字符串的长度为零 ,判断这个变量或者文件内有数据
if [ -f “$CATALINA_PID” ]; then

-f 该『文件名』是否为文件(file)?(常用)

if [ -s “CATALINA_PID” ]; then  
  #-s 侦测该文件名是否为『非空白文件』?  
echo “Existing PID file found during start.”  
if [ -r “
CATALINA_PID” ]; then

-r 侦测该文件名是否具有『可读』的属性?

以上if语句主要判断这个$CATALINA_PID,是否能正常可读

PID=cat "$CATALINA_PID"
ps -p PID >/dev/null 2>&1  
  if [
? -eq 0 ] ; then
#判断这个PID现在有没有被占用,有就输出这个tomcat依旧在运行,退出
#上面这段语句功能非常好,可以判断出这个PID是否还在运行
echo “Tomcat appears to still be running with PID PID.Startaborted.exit1elseechoRemoving/clearingstalePIDfile.rmf CATALINA_PID” >/dev/null 2>&1

删除了这个$CATALINA_PID文件

if [ ?!=0];thenif[w CATALINA_PID” ]; then
#-w 侦测该文件名是否具有『可写』的属性?
cat /dev/null > “$CATALINA_PID”

清空$CATALINA_PID文件

else
#如果不具有可写权限,则输出信息,退出
echo “Unable to remove or clear stale PID file. Start aborted.”
exit 1
fi
fi
fi
else

如果”$CATALINA_PID”没有可读权限,任然输出信息退出。

echo “Unable to read PID file. Start aborted.”
exit 1
fi
else
#” CATALINAPIDrmf CATALINA_PID” >/dev/null 2>&1

强制删除$CATALINA_PID文件

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

这段语句,主要目的就是清空$CATALINA_PID文件,如果出现不可读写,则输出信息退出

shift
#作废掉前面参数, 1touch CATALINA_OUT”
#创建 CATALINAOUTif[ 1” = “-security” ] ; then
#如果第二个参数是-security
if [ havettyeq1];thenechoUsingSecurityManagerfishift _RUNJAVA" " LOGGINGCONFIG JAVA_OPTS CATALINAOPTS Djava.endorsed.dirs= JAVA_ENDORSED_DIRS" -classpath " CLASSPATH Djava.security.manager Djava.security.policy== CATALINA_BASE”/conf/catalina.policy \
-Dcatalina.base=” CATALINABASE Dcatalina.home= CATALINA_HOME” \
-Djava.io.tmpdir=” CATALINATMPDIR org.apache.catalina.startup.Bootstrap @” start \

CATALINA_OUT” 2>&1 &  
      else  
      #我们平时执行启动tomcat核心就是这一句了  
      #  
    “
_RUNJAVA" " LOGGINGCONFIG JAVA_OPTS CATALINAOPTS Djava.endorsed.dirs= JAVA_ENDORSED_DIRS" -classpath " CLASSPATH Dcatalina.base= CATALINA_BASE” \
-Dcatalina.home=” CATALINAHOME Djava.io.tmpdir= CATALINA_TMPDIR” \
org.apache.catalina.startup.Bootstrap “ @start  CATALINA_OUT” 2>&1 &

从&可以看出启动的命令在后台启动

fi
if [ ! -z “$CATALINA_PID” ]; then

判断CATALINA_PID如果不是空字符,则将Shell最后运行的后台Process的PID 传给$CATALINA_PID

echo !>" CATALINA_PID”

在使用命令运行进程至后台时,可以使用$!抓取前面启动运行在后台进程的进程号

fi
fi

下面开始了tomcat停止模块了

elif [ “$1” = “stop” ] ; then
shift
SLEEP=5

if [ ! -z “ 1];thenecho 1 | grep “[^0-9]” >/dev/null 2>&1
if [ ?gt0];thenSLEEP= 1
shift
fi
fi
#上面语句主要是判断停止语句执行到此时,设定几秒后再执行停止语句
#用来配合stop n

FORCE=0
if [ “$1” = “-force” ]; then
shift
FORCE=1
fi
#如果参数中使用了-force,则FORCE=1

if [ ! -z “CATALINA_PID” ]; then  
  #
CATALINA_PID文件不是非空
if [ -f “ CATALINAPID];thenif[s CATALINA_PID” ]; then
kill -0 cat "$CATALINA_PID" >/dev/null 2>&1

kill -0 pid 不发送任何信号,但是系统会进行错误检查。

    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
#以上脚本是进行停止命令检错的,如果可能停止不了,则可以直接报错

RUNJAVA" JAVA_OPTS \
-Djava.endorsed.dirs=” JAVAENDORSEDDIRS"classpath" CLASSPATH” \
-Dcatalina.base=” CATALINABASE Dcatalina.home= CATALINA_HOME” \
-Djava.io.tmpdir=” CATALINATMPDIR org.apache.catalina.startup.Bootstrap @” stop

这个就是停止脚本的核心命令了

if [ ! -z “ CATALINAPID];thenif[f CATALINA_PID” ]; then
while [ $SLEEP -ge 0 ]; do
kill -0 cat "$CATALINA_PID" >/dev/null 2>&1
if [ ?gt0];thenrmf CATALINA_PID” >/dev/null 2>&1
if [ ?!=0];thenif[w CATALINA_PID” ]; then
cat /dev/null > “ CATALINAPIDelseechoTomcatstoppedbutthePIDfilecouldnotberemovedorcleared.fifibreakfiif[ SLEEP -gt 0 ]; then
sleep 1
fi
if [ SLEEPeq0];thenif[ FORCE -eq 0 ]; then
echo “Tomcat did not stop in time. PID file was not removed.”
fi
fi
SLEEP=expr $SLEEP - 1
done
fi
fi
#上段语句主要是清空$CATALINA_PID

#值得注意的是,生产环境偶尔不加-force选项,tomcat有时无法停止下来
#如果参数带”-force” ,则强制kill掉tomcat
if [ FORCEeq1];thenif[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: PIDkill9 PID

强制执行的核心命令

    rm -f "$CATALINA_PID" >/dev/null 2>&1
    if [ $? != 0 ]; then
      echo "Tomcat was killed but the PID file could not be removed."
    fi
  fi
fi

fi

查看版本参数

elif [ “ 1=version];then _RUNJAVA” \
-classpath “ CATALINAHOME/lib/catalina.jar org.apache.catalina.util.ServerInfoelseechoUsage:catalina.sh(commands)echocommands: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 ” 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

http://www.2cto.com/os/201503/384698.html

四、 setclasspath.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.

—————————————————————————–

Set CLASSPATH and Java options

#

Id:setclasspath.sh7950372009071710:52:16Zmarkt

—————————————————————————–

Make sure prerequisite environment variables are set

因为setclasspath.sh脚本是被catalina.sh调用,所以可以继承catalina.sh中的变量申明

if [ -z “ JAVAHOME"az" JRE_HOME” ]; then

判断用户有没有提前做 JAVAHOME JRE_HOME全局变量声明,如果都没进行申明

# Bugzilla 37284 (reviewed).
if $darwin; then
#要理解这个判断,先看下startup.sh和shutdown.sh就会明白
#这个是win仿真unix不用管下面两个语句
if [ -d “/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home” ]; then
export JAVA_HOME=”/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home”
fi
else
#其他环境没有申明,那么系统自己想办法找这两个变量的路径
JAVA_PATH=which java 2>/dev/null

此语句可以把java命令位置找出来

if [ "x$JAVA_PATH" != "x" ]; then

如果能找出java路径,则可以定位到java命令的路径,经过作者验证不是java的装路径

所以通过此处就可以看出,老鸟们为什么都要自己指定这两个变量了

  JAVA_PATH=`dirname $JAVA_PATH 2>/dev/null`
  JRE_HOME=`dirname $JAVA_PATH 2>/dev/null`
fi
if [ "x$JRE_HOME" = "x" ]; then

如果找不到java路径,那么就看有没有/usr/bin/java这个执行文件,有的话就它了,没有就算了

  # XXX: Should we try other locations?
  if [ -x /usr/bin/java ]; then
    JRE_HOME=/usr
  fi
fi

fi

if [ -z “ JAVAHOME"az" JRE_HOME” ]; then
#再验证一边,有没有这两个变量,没有不好意思,我不执行了,退出
#这个exit 1 不但是结束setclasspath.sh,会彻底退出catalina.sh脚本的
#对于在脚本中引用脚本的童鞋们,就需要注意了,小心使用exit。
echo “Neither the JAVA_HOME nor the JRE_HOME environment variable is defined”
echo “At least one of these environment variable is needed to run this program”
exit 1
fi
fi
if [ -z “ JAVAHOME"a" 1” = “debug” ]; then
echo “JAVA_HOME should point to a JDK in order to run in debug mode.”
exit 1
fi
if [ -z “ JREHOME];thenJREHOME= JAVA_HOME”
fi

If we’re running under jdb, we need a full jdk.

if [ “ 1=debug];thenif[ os400” = “true” ]; then
if [ ! -x “ JAVAHOME"/bin/javao!x" JAVA_HOME”/bin/javac ]; then
echo “The JAVA_HOME environment variable is not defined correctly”
echo “This environment variable is needed to run this program”
echo “NB: JAVA_HOME should point to a JDK not a JRE”
exit 1
fi
else
if [ ! -x “ JAVAHOME"/bin/javao!x" JAVA_HOME”/bin/jdb -o ! -x “$JAVA_HOME”/bin/javac ]; then
echo “The JAVA_HOME environment variable is not defined correctly”
echo “This environment variable is needed to run this program”
echo “NB: JAVA_HOME should point to a JDK not a JRE”
exit 1
fi
fi
fi

上段的代码都是在确认 JAVAHOME JRE_HOME变量的申明情况及后续的解决过程

if [ -z “$BASEDIR” ]; then

对”$BASEDIR变量的检查,木有的话就退出

echo “The BASEDIR environment variable is not defined”
echo “This environment variable is needed to run this program”
exit 1
fi
if [ ! -x “$BASEDIR”/bin/setclasspath.sh ]; then

确认”$BASEDIR”/bin/setclasspath.sh有木有,木有还是退出

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

eval不清楚嘛意思

else
echo “The BASEDIR environment variable is not defined correctly”
echo “This environment variable is needed to run this program”
exit 1
fi
fi

Don’t override the endorsed dir if the user has set it previously

这个是确认JAVA_ENDORSED_DIRS的位置

if [ -z “JAVA_ENDORSED_DIRS” ]; then  
  # Set the default -Djava.endorsed.dirs argument  
  JAVA_ENDORSED_DIRS=”
BASEDIR”/endorsed
fi

OSX hack to CLASSPATH

JIKESPATH=
if [ uname -s = “Darwin” ]; then
OSXHACK=”/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Classes”
if [ -d “ OSXHACK];thenforiin OSXHACK”/*.jar; do
JIKESPATH=” JIKESPATH":" i”
done
fi
fi

Set standard commands for invoking Java.

这句是响当当的重要,确定了$_RUNJAVA的值

_RUNJAVA=” JREHOME/bin/javaif[ os400” != “true” ]; then
_RUNJDB=”$JAVA_HOME”/bin/jdb
fi

http://www.2cto.com/os/201503/384700.html
五、 centos6下,Tomcat6自启动设置
1、修改start.sh文件
vim /home/wwwroot/tomcat_wiki/bin/startup.sh
在文件头增加以下内容:

!/bin/sh

chkconfig: 2345 97 00

description:tomcat auto start

processname: tomcat

2、修改catalina.sh文件
vim /home/wwwroot/tomcat_wiki/bin/catalina.sh
增加以下内容:
export CATALINA_BASE=/home/wwwroot/tomcat_wiki
export CATALINA_HOME=/home/wwwroot/tomcat_wiki
export CATALINA_TMPDIR=/home/wwwroot/tomcat_wiki/temp
export JRE_HOME=/usr/local/jdk1.6.0_37
3、创建链接文件
ln –s /home/wwwroot/tomcat_wiki/bin/startup.sh /etc/init.d/tomcat
4、修改权限
chmod +x tomcat
5、添加启动
chkconfig –add tomcat(add前是两个减号)
chkconfig tomcat on
6、检查
service tomcat start
或者reboot

猜你喜欢

转载自blog.csdn.net/chapterchou/article/details/62422289