Linux下tomcat服务启动/停止/重启脚本

适用情况:有多个tomcat同时使用,监听不同的端口,如在/usr/local下有多个tomcat目录名字依次是tomcat1、tomcat2、tomcat3....tomcat12

使用service tomcat 的方式启动停止tomcat服务,使用标签代表一个或几个tomcat服务,如:

boofm 代表 tomcat1、tomcat2、tomcat3,这样启动的时候就可以这么启动

 
  
  1. service tomcat bookfm start 

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

 
  
  1. service tomcat all start 

把脚本复制到/etc/init.d/下,命名为tomcat,就可以实现service 的方式来启动tomcat服务

效果如下:

使用方式:service tomcat xxx start|stop|restart|status

 
  
  1. [root@server1 ~]# service tomcat bookfm start  
  2. tomcat1 starting,Please wait 2s...  
  3. tomcat2 starting,Please wait 2s...  
  4. tomcat3 starting,Please wait 2s...  
  5. [root@server1 ~]#  
 
  
  1. [root@server1 ~]# service tomcat bookfm stop  
  2. tomcat1 stopping,Please wait 2s...  
  3. tomcat2 stopping,Please wait 2s...  
  4. tomcat3 stopping,Please wait 2s...  
  5. [root@server1 ~]#  
 
  
  1. [root@server1 ~]# service tomcat bookfm restart  
  2. tomcat1 is not running...  
  3. tomcat1 starting,Please wait 2s...  
  4. tomcat2 is not running...  
  5. tomcat2 starting,Please wait 2s...  
  6. tomcat3 is not running...  
  7. tomcat3 starting,Please wait 2s... 
 
  
  1. [root@server1 ~]# service tomcat bookfm status  
  2. tomcat1 is running  
  3. tomcat2 is running  
  4. tomcat3 is running  
  5. [root@server1 ~]#  

第二版

 
  
  1. #!/bin/bash 
  2.   
  3.  # Apache Tomcat daemon 
  4.  # 
  5.  # chkconfig: 345 10 10 
  6.  # description: Apache Tomcat daemon 
  7.  # 
  8.  # processname: tomcat 
  9.   
  10.  export JAVA_HOME=/usr/local/java 
  11.   
  12.  JAVA_RUN="/usr/local/java/bin/java" 
  13.   
  14.  usage="{bookfm|edumh|all} {start|stop|restart|status}" 
  15.   
  16.  JAVA_OPTS="" 
  17.  #JAVA_OPTS="-server -Xms512m -Xmx512m \ 
  18.  #           -XX:PermSize=16m -XX:MaxPermSize=64m -XX:MaxNewSize=64m \ 
  19.  #           -XX:-UseGCOverheadLimit -XX:+UseConcMarkSweepGC" 
  20.   
  21.  #judge $1 $2 whether null 
  22.  if [ "$1" == "" -o "$2" == "" ];then 
  23.       echo 
  24.       echo "Usage: $0 $usage" 
  25.       echo 
  26.       exit 1 
  27.  fi 
  28.   
  29.  #judge $1 
  30.  case $1 in 
  31.          "bookfm"
  32.                  tomcats="1 2 3" 
  33.                          ;; 
  34.          "edumh"
  35.                  tomcats="12" 
  36.                          ;; 
  37.          "all"
  38.                  tomcats="1 2 3 12" 
  39.                          ;; 
  40.          *) 
  41.            echo "Usage: $0 $usage" 
  42.                 ;; 
  43.  esac 
  44.   
  45.  tab=$1 
  46.   
  47.  #define start function 
  48.  tomcatstart() { 
  49.       for i in $tomcats 
  50.       do 
  51.            TOMCAT="tomcat$i" 
  52.            run_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties"
  53.       LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \ 
  54.                            -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" 
  55.       JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed" 
  56.       CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar" 
  57.       CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \ 
  58.                           -Dcatalina.home=/usr/local/$TOMCAT" 
  59.           CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp" 
  60.           CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out" 
  61.       cd /usr/local/$TOMCAT 
  62.            if [ "${run_status}X" != "X" ];then 
  63.                 echo "$tab==>$TOMCAT is already running..." 
  64.            else 
  65.            $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \ 
  66.                org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 & 
  67.                echo $! > /tmp/$TOMCAT.pid 
  68.                 echo "$tab==>$TOMCAT starting,Please wait 2s..." 
  69.                 sleep 2 
  70.            fi 
  71.       done 
  72.  } 
  73.   
  74.  #define stop function 
  75.  tomcatstop() { 
  76.       for j in $tomcats 
  77.       do 
  78.            TOMCAT="tomcat$j" 
  79.            run1_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties"
  80.            if [ "${run1_status}X" == "X" ];then 
  81.                 echo "$TOMCAT is not running..." 
  82.            else 
  83.            kill -9 `cat /tmp/$TOMCAT.pid` 
  84.                 echo "$TOMCAT stopping,Please wait 2s..." 
  85.                 sleep 2 
  86.            fi 
  87.       done 
  88.  } 
  89.   
  90.  #define restart function 
  91.  tomcatrestart() { 
  92.   
  93.       for m in $tomcats 
  94.       do 
  95.            TOMCAT="tomcat$m" 
  96.            run2_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties"
  97.       LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \ 
  98.                       -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" 
  99.       JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed" 
  100.       CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar" 
  101.       CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \ 
  102.                  -Dcatalina.home=/usr/local/$TOMCAT" 
  103.       CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp" 
  104.       CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out" 
  105.   
  106.   
  107.            if [ "${run2_status}X" == "X" ];then 
  108.                 echo "$tab==>$TOMCAT is not running..." 
  109.            $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \ 
  110.           org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 & 
  111.            echo $! > /tmp/$TOMCAT.pid 
  112.                 echo "$tab==>$TOMCAT starting,Please wait 2s..." 
  113.                 sleep 2 
  114.            else 
  115.            cd /usr/local/$TOMCAT 
  116.            kill -9 `cat /tmp/$TOMCAT.pid` 
  117.                 echo "$TOMCAT stopping,Please wait 2s..." 
  118.                 sleep 2 
  119.            $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \ 
  120.            org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 & 
  121.            echo $! > /tmp/$TOMCAT.pid 
  122.                 echo "$TOMCAT starting,Please wait 2s..." 
  123.                 sleep 2 
  124.            fi 
  125.       done 
  126.  } 
  127.   
  128.  #define status function 
  129.  tomcatstatus() { 
  130.       for n in $tomcats 
  131.       do 
  132.            TOMCAT="tomcat$n" 
  133.            run3_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties"
  134.            if [ "${run3_status}X" == "X" ];then 
  135.                 echo "$tab==>$TOMCAT is not running..."     
  136.            else 
  137.                 echo "$tab==>$TOMCAT is running" 
  138.            fi 
  139.       done 
  140.  } 
  141.   
  142.  #judge $2 
  143.  case $2 in 
  144.       "start"
  145.            tomcatstart 
  146.                 ;; 
  147.       "stop"
  148.            tomcatstop 
  149.                 ;; 
  150.       "restart"
  151.            tomcatrestart 
  152.                 ;; 
  153.       "status"
  154.            tomcatstatus 
  155.                 ;; 
  156.       *) 
  157.            echo "Usage: $0 $usage" 
  158.                 ;; 
  159.  esac 

 

第一版

脚本内容如下:

 
  
  1. #!/bin/bash  
  2.  
  3. # Apache Tomcat daemon  
  4. #  
  5. # chkconfig: 345 10 10  
  6. # description: Apache Tomcat daemon  
  7. #  
  8. # processname: tomcat  
  9.  
  10. export JAVA_HOME=/usr/local/java  
  11.  
  12. #define variables  
  13. tom="/usr/local/tomcat" 
  14. startup_bin="bin/startup.sh" 
  15. shutdown_bin="bin/shutdown.sh" 
  16. usage="{bookfm|edumh|all} {start|stop|restart|status}" 
  17. dev="/dev/null" 
  18.  
  19. #judge $1 $2 whether null  
  20. if [ "$1" == "" -o "$2" == "" ];then  
  21.     echo  
  22.     echo "Usage: $0 $usage"  
  23.     echo   
  24.     exit 1  
  25. fi  
  26.  
  27. #judge $1  
  28. case $1 in  
  29.         "bookfm")  
  30.                 tomcats="1 2 3" 
  31.                         ;;  
  32.         "edumh")  
  33.                 tomcats="12" 
  34.                         ;;  
  35.         "all")  
  36.                 tomcats="1 2 3 12" 
  37.                         ;;  
  38.         *)  
  39.         echo "Usage: $0 $usage"  
  40.             ;;  
  41. esac  
  42.  
  43. #define start function  
  44. tomcatstart() {  
  45.     for i in $tomcats  
  46.     do  
  47.         tom_home="$tom$i" 
  48.         run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")  
  49.         if [ "${run_status}X" != "X" ];then  
  50.             echo "tomcat$i is already running..."  
  51.         else  
  52.             ${tom_home}/${startup_bin} &>$dev  
  53.             echo "tomcat$i starting,Please wait 2s..."  
  54.             sleep 2  
  55.         fi  
  56.     done  
  57. }  
  58.  
  59. #define stop function  
  60. tomcatstop() {  
  61.     for j in $tomcats  
  62.     do  
  63.         tom1_home="$tom$j" 
  64.         run1_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom1_home}")  
  65.         if [ "${run1_status}X" == "X" ];then  
  66.             echo "tomcat$j is not running..."  
  67.         else  
  68.             ${tom1_home}/${shutdown_bin} &>$dev  
  69.             echo "tomcat$j stopping,Please wait 2s..."  
  70.             sleep 2  
  71.         fi  
  72.     done  
  73. }  
  74.  
  75. #define restart function  
  76. tomcatrestart() {  
  77.  
  78.     for m in $tomcats  
  79.     do  
  80.         tom2_home="$tom$m" 
  81.         run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")  
  82.         if [ "${run2_status}X" == "X" ];then  
  83.             echo "tomcat$m is not running..."  
  84.             ${tom2_home}/${startup_bin} &>$dev  
  85.             echo "tomcat$m starting,Please wait 2s..."  
  86.             sleep 2  
  87.         else  
  88.             ${tom2_home}/${shutdown_bin} &>$dev  
  89.             echo "tomcat$m stopping,Please wait 2s..."  
  90.             sleep 2  
  91.             ${tom2_home}/${startup_bin} &>$dev  
  92.             echo "tomcat$m starting,Please wait 2s..."  
  93.             sleep 2  
  94.         fi  
  95.     done  
  96. }  
  97.  
  98. #define status function  
  99. tomcatstatus() {  
  100.     for n in $tomcats  
  101.     do  
  102.         tom3_home="$tom$n" 
  103.         run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")  
  104.         if [ "${run3_status}X" == "X" ];then  
  105.             echo "tomcat$n is not running..."     
  106.         else  
  107.             echo "tomcat$n is running"  
  108.         fi  
  109.     done  
  110. }  
  111.  
  112. #judge $2   
  113. case $2 in  
  114.     "start")  
  115.         tomcatstart  
  116.             ;;  
  117.     "stop")  
  118.         tomcatstop  
  119.             ;;  
  120.     "restart")  
  121.         tomcatrestart  
  122.             ;;  
  123.     "status")  
  124.         tomcatstatus  
  125.             ;;  
  126.     *)  
  127.         echo "Usage: $0 $usage"  
  128.             ;;  
  129. esac