管理多tomcat实例的shell脚本

为了简化tomcat的部署复杂度, 把以前单独配置的脚本提出来做了一个带参数的, 可以用于单机环境下的多tomcat实例管理.

其中deploy功能, 会将指定的模块war按日期时间更名备份后, 再部署新的war

命令行格式为

tc.sh [tomcat_location] {status|start|stop|restart|jstat|deploy} [module_name]

脚本代码

# Please define the absolute path of tomcat instance
JAVA_HOME=/opt/java/jdk1.8.0_101
TC_BASE=/home/tomcat
BUILD_FOLDER=/home/tomcat/builds

status() {
  echo -n "[$(date +%Y%m%d-%H%M%S)] "
  pid=`ps -ef|grep ${CATALINA_BASE}|grep -v ‘grep‘|grep ‘java‘|awk ‘{print $2}‘`
  if [ -z ${pid} ] ; then
    echo $1‘ is not running‘
  else
    echo $1‘ is running at pid: ‘${pid}
  fi
}

start() {
  echo -n "[$(date +%Y%m%d-%H%M%S)] Start: "
  pid=`ps -ef|grep ${CATALINA_BASE}|grep -v ‘grep‘|grep ‘java‘|awk ‘{print $2}‘`
  if [ -z ${pid} ] ; then
    echo $1
    . ${CATALINA_BASE}/bin/startup.sh
  else
    echo $1‘ is running at pid: ‘${pid}
  fi
}

stop() {
  if [ -z $1 ]; then
    exit 1
  fi

  echo -n "[$(date +%Y%m%d-%H%M%S)] Stop: "
  pid=`ps -ef|grep ${CATALINA_BASE}|grep -v ‘grep‘|grep ‘java‘|awk ‘{print $2}‘`
  if [ -z $pid ] ; then
    echo $1‘ is not running‘
  else
    echo $1‘ is running at pid:‘${pid}
    . ${CATALINA_BASE}/bin/shutdown.sh
    sleep 5
  fi

  pid=`ps -ef|grep ${CATALINA_BASE}|grep -v ‘grep‘|grep ‘java‘|awk ‘{print $2}‘`
  if [ -z $pid ] ; then
    echo ‘Done‘
  else
    echo ‘Still running, now killing with pid:‘${pid}
    ps -ef|grep ${CATALINA_BASE}|grep -v ‘grep‘|grep ‘java‘|awk ‘{print $2}‘|xargs kill -s 9
  fi
}

restart() {
  stop $1
  sleep 1
  start $1
}

deploy() {
  if [ -z $2 ]; then
    echo $"Usage: $0 $1 deploy [module_name]"
    exit 2
  fi
  [ -f ${BUILD_FOLDER}/$2.war ] || exit 1

  stop $1
  df -h
  echo ‘‘

  local BACKUP_FILENAME=$2-$(date +%Y%m%d-%H%M).war
  echo -n "[$(date +%Y%m%d-%H%M%S)] Deploy: "
  echo -n "backup ${BACKUP_FILENAME}... "
  mv ${CATALINA_BASE}/webapps/$2.war ${BUILD_FOLDER}/${BACKUP_FILENAME}
  echo -n "clean... "
  rm -rf ${CATALINA_BASE}/webapps/$2
  echo -n "copy... "
  cp -f ${BUILD_FOLDER}/$2.war ${CATALINA_BASE}/webapps/
  echo ‘done‘

  start $1
}

jstat() {
  if [ -z $1 ]; then
    exit 1
  fi
  local CATALINA_BASE=${TC_BASE}/$1
  [ -d ${CATALINA_BASE} ] || exit 1
  echo -n "[$(date +%Y%m%d-%H%M%S)] jstat: "
  pid=`ps -ef|grep ${CATALINA_BASE}|grep -v ‘grep‘|grep ‘java‘|awk ‘{print $2}‘`
  if [ -z $pid ] ; then
    echo $1‘ is not running‘
  else
    echo $1‘ is running at pid:‘${pid}
    ${JAVA_HOME}/bin/jmap -heap ${pid}
    echo ‘‘
    ${JAVA_HOME}/bin/jstat -gcutil ${pid} 1000 10
  fi
}

if [ -z $1 ]; then
  echo $"Usage: $0 [tomcat_location] {status|start|stop|restart|jstat|deploy} [module_name]"
  exit 2
else
  target=${1%/}
fi

CATALINA_BASE=${TC_BASE}/$target
[ -d ${CATALINA_BASE} ] || exit 1

case "$2" in
    status)
        $2 $target
        ;;
    start)
        $2 $target
        ;;
    stop)
        $2 $target
        ;;
    restart)
        $2 $target
        ;;
    jstat)
        $2 $target
        ;;
    deploy)
        $2 $target $3
        ;;
    *)
        echo $"Usage: $0 [tomcat_location] {status|start|stop|restart|jstat|deploy} [module_name]"
        exit 2
esac

对应的tomcat实例内部的startup.sh 和 shutdown.sh, 只需要配置jvm参数和tomcat源路径

startup.sh

JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:MaxNewSize=512m -Djava.awt.headless=true"
CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.36
. $CATALINA_HOME/bin/catalina.sh start

shutdown.sh

CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.36
. $CATALINA_HOME/bin/catalina.sh stop
时间: 2024-10-29 04:24:49

管理多tomcat实例的shell脚本的相关文章

实用脚本----Linux下Jdk和Tomcat自动安装shell脚本总结

系统环境为:ubuntu 14.04 一.JDK 自动安装脚本 jdk自动安装bash shell脚本,截止今天(2014/10/15)亲测可用: sudo su #切换到root权限 mkdir /usr/local/java cd /usr/local/java #download jdk 1.7.67 wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co

一台服务器上多tomcat实例的启动脚本

公司有一台服务器起了多个tomcat,每次需要重启,查看catalina.out输出都特别麻烦,写了一个脚本,贴出来,希望有经验的前辈指点一二,脚本如下: #!/bin/sh echo "请选择要操作的tomcat实例:" echo "1 : tomcat" echo "2 : tomcat2" echo "3 : tomcat_errorQues" tomcatid=`ps aux | grep java | grep -v

自动清除过期的Tomcat日志的shell脚本

(友好提示:本文属于初级shell编程,高手可忽略此文) 在产品环境运行过程中,每天会切割产生按天计的日志文件,这些日志文件一般在过一定期限以后就没什么用处了,因此需要定期删除掉这些过期的日志文件.本文以CentOS下的Tomcat为应用环境基础,通过shell脚本的方式定时自动删除过期的Tomcat日志文件,以减少磁盘空间的占用.减轻管理员的日常工作. 一.shell脚本代码: #!/bin/bash # # filename: clearExpiredTomcatLogs.sh # # FU

Dockerfile 构建后端tomcat应用并用shell脚本实现jenkins自动构建

Dockerfile 文件构建docker镜像 FROM centos MAINTAINER zhaoweifeng "zh******tech.cn" ENV LANG en_US.UTF-8 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone WORKDIR /home RUN mkdir JDK COPY jdk

Tomcat灰度发布shell脚本(来自网络可以参考)

java灰度脚本 使用脚本注意事项:1. 发布机器需要能够解析web服务器主机名,并且配置ssh通信2. 变量中的目录以及用户等信息需要自己创建,脚本没有做判断自己创建.我这里web服务器是使用ansible进行部署的,相关目录和用户都会自动创建.3.代码的部署使用tag,但是代码的更新使用软连接来控制,回滚则切换到上一个软连接4.由于java是编译型语言,我们使用maven来进行编译,所以需要安装maven环境.5.关于环境配置文件:配置文件为自己手动维护,每次都是删除git仓库拉取下来的配置

tomcat 日志切割shell脚本

vim tomcat_cut.sh #!/bin/bash log_path=/opt/tomcat8080/logs d=`date +%Y-%m-%d` d4=`date -d'30 day ago' +%Y-%m-%d` cd ${log_path} && cp catalina.out $log_path/catalina.out.$d.log echo > catalina.out rm -rf $log_path/catalina.out.${d4}.log # chmo

快速部署tomcat项目的Shell脚本

为了做集群测试,在每台机器上装了3个tomcat,每次发布项目的时候都要反复敲一些命令,重启tomcat之前先检查tomcat进程有没有停掉,没有还要手动kill该进程. 发布次数多了,操作就比较繁琐了,索性写个脚本一键发布,省时省力^_^. 把deploy.sh和restart.sh分别拷贝到3个tomcat的bin目录下,再用chmod +x给这两个脚本赋上可执行权限. 一键发布命令:./deploy.sh 项目war包    例如:./deploy.sh /home/test.war 说明

Linux 启动、停止、重启tomcat工具(Shell脚本)

1.   启动 #!/bin/bash pids=`ps -ef | grep java | grep -w tomcat | awk '{print $2}'` #pids=`ps -ef | grep -w tomcat | grep -v "grep" | awk '{print $2}'` if test -z $pids then bash /root/soft/tomcat/bin/startup.sh echo -e "Start Finished!\n&quo

shell脚本调度 Tomcat 启动、停止

Tomcat启动: shell脚本调度tomcat时,必须将java home添加到环境变量中,否则无法调用成功 #!/bin/bashexport JAVA_HOME=/usr/lib/jvm/jdk1.8.0_231echo "The execute stime is:"`date` >> execute.logsh /root/apache-tomcat-7.0.94/bin/startup.sh Tomcat停止: 用tomcat自带的shutdown.sh有时不起