阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Tomcat停止脚本shutdown.sh分析

101次阅读
没有评论

共计 2880 个字符,预计需要花费 8 分钟才能阅读完成。

一、分析说明
    为了写出更加完善的 tomcat 启动方面的脚本,用于代码上线自动化,特分析下 tomcat 的 bin 目录下的 shutdown.sh 脚本,学习标准的 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.

# —————————————————————————–
# Stop script for the CATALINA Server
#
# $Id: shutdown.sh 1130937 2011-06-03 08:27:13Z markt $
# —————————————————————————–

# resolve links – $0 may be a softlink
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
# 上面循环语句的意思是保证文件路径不是一个连接,使用循环直至找到文件原地址
# 遇到一时看不明白的 shell,可以拆解后自己在 linux 反复运行验证,一点点拆解就会明白的
#link=`expr “$ls” : ‘.*-> \(.*\)$’` 模拟后:expr ‘lrwxrwxrwx 1 root root 19 3 月  17 10:12 ./bbb.sh -> /root/shell/test.sh’ : ‘.*-> \(.*\)$’
# 很明确的发现是用 expr 来提取 /root/shell/test.sh 的内容

# 获取这个脚本的目录
PRGDIR=`dirname “$PRG”`
EXECUTABLE=catalina.sh

 

# Check that target executable exists
if [! -x “$PRGDIR”/”$EXECUTABLE”]; then
  echo “Cannot find $PRGDIR/$EXECUTABLE”
    #判断脚本 catalina.sh 是否存在并有可执行权限,没有执行权限就退出
  echo “The file is absent or does not have execute permission”
  echo “This file is needed to run this program”
  exit 1
fi

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

三、总结
    解读完 startup.sh 脚本,就会发现 shutdown.sh 与 startup.sh 结构及其相同,tomcat 的 shutdown.sh 主要目的找到 catalina.sh 脚本位置,将启动命令参数传递给 catalina.sh 执行。然而 catalina.sh 脚本中也涉及到判断系统环境和找到 catalina.sh 脚本原路径的相关代码,所以执行 tomcat 启动时,无需使用 startup.sh 脚本(下一篇分析的 shutdown.sh 也类似),直接./catalina.sh start|stop|restart 即可。
    后一篇,作者将分析 catalina.sh 脚本执行原理,从而充分认识到 tomcat 的启动过程和需要注意细节,为 tomcat 的使用和排除提供知识储备,文中有误的地方,忘广大运维朋友们提出指正意见。

RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目  http://www.linuxidc.com/Linux/2015-02/113528.htm

Tomcat 权威指南 (第二版)(中英高清 PDF 版 + 带书签)  http://www.linuxidc.com/Linux/2015-02/113062.htm

Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm

Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm

CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm

CentOS 6.5 下安装 Tomcat  http://www.linuxidc.com/Linux/2015-01/111415.htm

Tomcat 的详细介绍 :请点这里
Tomcat 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-03/115205.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计2880字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中