设为首页 收藏本站
查看: 653|回复: 0

[经验分享] tomcat服务 启动/停止/重启脚本

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2018-11-29 14:01:31 | 显示全部楼层 |阅读模式
  Linux下tomcat服务启动/停止/重启脚本
  适用情况:有多个tomcat同时使用,监听不同的端口,如在/usr/local下有多个tomcat目录名字依次是tomcat1、tomcat2、tomcat3....tomcat12
  使用service tomcat 的方式启动停止tomcat服务,使用标签代表一个或几个tomcat服务,如:
  boofm 代表 tomcat1、tomcat2、tomcat3,这样启动的时候就可以这么启动


  • service tomcat bookfm start

  all 代表所有的tomcat,这样启动的时候就可以这么启动


  • service tomcat all start

  把脚本复制到/etc/init.d/下,命名为tomcat,就可以实现service 的方式来启动tomcat服务
  效果如下:
  使用方式:service tomcat xxx start|stop|restart|status


  • [root@server1 ~]# service tomcat bookfm start  
  • tomcat1 starting,Please wait 2s...  
  • tomcat2 starting,Please wait 2s...  
  • tomcat3 starting,Please wait 2s...  
  • [root@server1 ~]#  


  • [root@server1 ~]# service tomcat bookfm stop  
  • tomcat1 stopping,Please wait 2s...  
  • tomcat2 stopping,Please wait 2s...  
  • tomcat3 stopping,Please wait 2s...  
  • [root@server1 ~]#  


  • [root@server1 ~]# service tomcat bookfm restart  
  • tomcat1 is not running...  
  • tomcat1 starting,Please wait 2s...  
  • tomcat2 is not running...  
  • tomcat2 starting,Please wait 2s...  
  • tomcat3 is not running...  
  • tomcat3 starting,Please wait 2s...


  • [root@server1 ~]# service tomcat bookfm status  
  • tomcat1 is running  
  • tomcat2 is running  
  • tomcat3 is running  
  • [root@server1 ~]#  

  第二版


  • #!/bin/bash
  •   
  • # Apache Tomcat daemon
  • #
  • # chkconfig: 345 10 10
  • # description: Apache Tomcat daemon
  • #
  • # processname: tomcat
  •   
  • export JAVA_HOME=/usr/local/java
  •   
  • JAVA_RUN="/usr/local/java/bin/java"
  •   
  • usage="{bookfm|edumh|all} {start|stop|restart|status}"
  •   
  • JAVA_OPTS=""
  • #JAVA_OPTS="-server -Xms512m -Xmx512m \
  • #           -XX:PermSize=16m -XX:MaxPermSize=64m -XX:MaxNewSize=64m \
  • #           -XX:-UseGCOverheadLimit -XX:+UseConcMarkSweepGC"
  •   
  • #judge $1 $2 whether null
  • if [ "$1" == "" -o "$2" == "" ];then
  •       echo
  •       echo "Usage: $0 $usage"
  •       echo
  •       exit 1
  • fi
  •   
  • #judge $1
  • case $1 in
  •          "bookfm")
  •                  tomcats="1 2 3"
  •                          ;;
  •          "edumh")
  •                  tomcats="12"
  •                          ;;
  •          "all")
  •                  tomcats="1 2 3 12"
  •                          ;;
  •          *)
  •            echo "Usage: $0 $usage"
  •                 ;;
  • esac
  •   
  • tab=$1
  •   
  • #define start function
  • tomcatstart() {
  •       for i in $tomcats
  •       do
  •            TOMCAT="tomcat$i"
  •            run_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  •       LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \
  •                            -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
  •       JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed"
  •       CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar"
  •       CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \
  •                           -Dcatalina.home=/usr/local/$TOMCAT"
  •           CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp"
  •           CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out"
  •       cd /usr/local/$TOMCAT
  •            if [ "${run_status}X" != "X" ];then
  •                 echo "$tab==>$TOMCAT is already running..."
  •            else
  •            $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
  •                org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
  •                echo $! > /tmp/$TOMCAT.pid
  •                 echo "$tab==>$TOMCAT starting,Please wait 2s..."
  •                 sleep 2
  •            fi
  •       done
  • }
  •   
  • #define stop function
  • tomcatstop() {
  •       for j in $tomcats
  •       do
  •            TOMCAT="tomcat$j"
  •            run1_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  •            if [ "${run1_status}X" == "X" ];then
  •                 echo "$TOMCAT is not running..."
  •            else
  •            kill -9 `cat /tmp/$TOMCAT.pid`
  •                 echo "$TOMCAT stopping,Please wait 2s..."
  •                 sleep 2
  •            fi
  •       done
  • }
  •   
  • #define restart function
  • tomcatrestart() {
  •   
  •       for m in $tomcats
  •       do
  •            TOMCAT="tomcat$m"
  •            run2_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  •       LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \
  •                       -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
  •       JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed"
  •       CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar"
  •       CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \
  •                  -Dcatalina.home=/usr/local/$TOMCAT"
  •       CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp"
  •       CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out"
  •   
  •   
  •            if [ "${run2_status}X" == "X" ];then
  •                 echo "$tab==>$TOMCAT is not running..."
  •            $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
  •           org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
  •            echo $! > /tmp/$TOMCAT.pid
  •                 echo "$tab==>$TOMCAT starting,Please wait 2s..."
  •                 sleep 2
  •            else
  •            cd /usr/local/$TOMCAT
  •            kill -9 `cat /tmp/$TOMCAT.pid`
  •                 echo "$TOMCAT stopping,Please wait 2s..."
  •                 sleep 2
  •            $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
  •            org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
  •            echo $! > /tmp/$TOMCAT.pid
  •                 echo "$TOMCAT starting,Please wait 2s..."
  •                 sleep 2
  •            fi
  •       done
  • }
  •   
  • #define status function
  • tomcatstatus() {
  •       for n in $tomcats
  •       do
  •            TOMCAT="tomcat$n"
  •            run3_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  •            if [ "${run3_status}X" == "X" ];then
  •                 echo "$tab==>$TOMCAT is not running..."     
  •            else
  •                 echo "$tab==>$TOMCAT is running"
  •            fi
  •       done
  • }
  •   
  • #judge $2
  • case $2 in
  •       "start")
  •            tomcatstart
  •                 ;;
  •       "stop")
  •            tomcatstop
  •                 ;;
  •       "restart")
  •            tomcatrestart
  •                 ;;
  •       "status")
  •            tomcatstatus
  •                 ;;
  •       *)
  •            echo "Usage: $0 $usage"
  •                 ;;
  • esac

  第一版
  脚本内容如下:


  • #!/bin/bash  

  • # Apache Tomcat daemon  
  • #  
  • # chkconfig: 345 10 10  
  • # description: Apache Tomcat daemon  
  • #  
  • # processname: tomcat  

  • export JAVA_HOME=/usr/local/java  

  • #define variables  
  • tom="/usr/local/tomcat"
  • startup_bin="bin/startup.sh"
  • shutdown_bin="bin/shutdown.sh"
  • usage="{bookfm|edumh|all} {start|stop|restart|status}"
  • dev="/dev/null"

  • #judge $1 $2 whether null  
  • if [ "$1" == "" -o "$2" == "" ];then  
  •     echo  
  •     echo "Usage: $0 $usage"  
  •     echo   
  •     exit 1  
  • fi  

  • #judge $1  
  • case $1 in  
  •         "bookfm")  
  •                 tomcats="1 2 3"
  •                         ;;  
  •         "edumh")  
  •                 tomcats="12"
  •                         ;;  
  •         "all")  
  •                 tomcats="1 2 3 12"
  •                         ;;  
  •         *)  
  •         echo "Usage: $0 $usage"  
  •             ;;  
  • esac  

  • #define start function  
  • tomcatstart() {  
  •     for i in $tomcats  
  •     do  
  •         tom_home="$tom$i"
  •         run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")  
  •         if [ "${run_status}X" != "X" ];then  
  •             echo "tomcat$i is already running..."  
  •         else  
  •             ${tom_home}/${startup_bin} &>$dev  
  •             echo "tomcat$i starting,Please wait 2s..."  
  •             sleep 2  
  •         fi  
  •     done  
  • }  

  • #define stop function  
  • tomcatstop() {  
  •     for j in $tomcats  
  •     do  
  •         tom1_home="$tom$j"
  •         run1_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom1_home}")  
  •         if [ "${run1_status}X" == "X" ];then  
  •             echo "tomcat$j is not running..."  
  •         else  
  •             ${tom1_home}/${shutdown_bin} &>$dev  
  •             echo "tomcat$j stopping,Please wait 2s..."  
  •             sleep 2  
  •         fi  
  •     done  
  • }  

  • #define restart function  
  • tomcatrestart() {  

  •     for m in $tomcats  
  •     do  
  •         tom2_home="$tom$m"
  •         run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")  
  •         if [ "${run2_status}X" == "X" ];then  
  •             echo "tomcat$m is not running..."  
  •             ${tom2_home}/${startup_bin} &>$dev  
  •             echo "tomcat$m starting,Please wait 2s..."  
  •             sleep 2  
  •         else  
  •             ${tom2_home}/${shutdown_bin} &>$dev  
  •             echo "tomcat$m stopping,Please wait 2s..."  
  •             sleep 2  
  •             ${tom2_home}/${startup_bin} &>$dev  
  •             echo "tomcat$m starting,Please wait 2s..."  
  •             sleep 2  
  •         fi  
  •     done  
  • }  

  • #define status function  
  • tomcatstatus() {  
  •     for n in $tomcats  
  •     do  
  •         tom3_home="$tom$n"
  •         run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")  
  •         if [ "${run3_status}X" == "X" ];then  
  •             echo "tomcat$n is not running..."     
  •         else  
  •             echo "tomcat$n is running"  
  •         fi  
  •     done  
  • }  

  • #judge $2   
  • case $2 in  
  •     "start")  
  •         tomcatstart  
  •             ;;  
  •     "stop")  
  •         tomcatstop  
  •             ;;  
  •     "restart")  
  •         tomcatrestart  
  •             ;;  
  •     "status")  
  •         tomcatstatus  
  •             ;;  
  •     *)  
  •         echo "Usage: $0 $usage"  
  •             ;;  
  • esac  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-641240-1-1.html 上篇帖子: linux tomcat的tomcat 下篇帖子: Zabbix利用JMX监控多实例Tomcat运行状态
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表