为了简化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