-------------------------------------------------[翠花,上腳本]-------------------------------------------------------------------------------
1 #!/bin/sh 2 # 3 # function: Tomcat mutil instance init script for Linux. 4 # author: 5 # QQ:765482322 6 # mail: login_532_gajun@sina.com 7 # notice: 8 # 1.若是tomcat實例是在同一個目錄下,那麼只須要修改此腳本中 "export CATALINA_BASE=(path)/$instance_name" 9 # 中的path爲你全部實例的父目錄便可,--instance= 選項就可使用相對路徑 10 # 2.若是tomcat實例不在同一個目錄下,那麼只須要修改此腳本中 "export CATALINA_BASE=$instance_name" 11 # --instance= 選項後面就得跟實例的絕對路徑 12 # 3.把此腳本放到/usr/local/bin/ 並重名命爲tomcat-multi,注意修改其餘用戶有執行權限 13 14 # 定義java的相關變量 15 export JAVA_HOME=/usr/java/latest 16 17 # define function 18 # 腳本命令使用語法檢測 19 syntax_check(){ 20 if [ $arg_count -ne 2 ];then 21 help_info 22 exit 1 23 fi 24 option && action || (help_info;exit 1) 25 } 26 27 # 打印腳本使用幫助信息 28 help_info(){ 29 cat|grep --color ".*" << EOF 30 Usage: tomcat-multil <OPTION>... <action> 31 32 OPTION: 33 --instance=NAME Add an instance Name must be an instance of the home directory 34 35 action: 36 start Run an instance 37 stop Stop an instance 38 restart Restart an instance 39 configtest Check configure file 40 status Show an instance status 41 print_log Print tomcat instance run log 42 EOF 43 } 44 45 # 根據給定的參數判斷用戶對tomcat實例所要執行的動做 46 action(){ 47 run_action=("start" "stop" "restart" "configtest" "status" "print_log") 48 local i=1 49 while [ $i -le 2 ];do 50 for j in ${run_action[*]};do 51 if [ ${arg[$i-1]} == $j ];then 52 hit_action=$j;break 53 else 54 continue 55 fi 56 done 57 if [ -n "$hit_action" ];then 58 #echo "hit_action=$hit_action" 59 break 60 else 61 let i++ 62 fi 63 done 64 if [ -z "$hit_action" ];then 65 help_info 66 exit 1 67 fi 68 69 } 70 71 # 經過選項--instance 獲得tomcat實例所在路徑 72 option() { 73 local i=1 74 while [ $i -le 2 ];do 75 if echo ${arg[$i-1]} | egrep -i "(^--instance=)" &> /dev/null;then 76 instance_name=$(echo ${arg[$i-1]} | sed -r 's/--instance=(.*)/\1/') 77 break 78 else 79 let i++ 80 fi 81 done 82 if [ -z "$instance_name" ];then 83 help_info 84 exit 1 85 fi 86 } 87 88 # 啓動tomcat實例 89 start(){ 90 $CATALINA_HOME/bin/startup.sh &> /dev/null 91 sleep 2 92 if ps -ef | grep "java" | grep -w "$CATALINA_HOME" &> /dev/null ;then 93 echo -e "Starting tomcat instance $instance_name: \033[32m[ OK ]\033[0m" 94 # echo $tomcat_getpid | bash > $pidfile 95 else 96 echo -e "Starting tomcat instance $instance_name: \033[31m[FAILED]\033[0m" 97 exit 1 98 fi 99 } 100 101 # 中止tomcat實例,注意有時使用catalina.sh 執行 stop不成功,因此加了層判斷,若是使用catalina.sh 關閉不了進程,就使用kill殺死進程 102 stop(){ 103 $CATALINA_HOME/bin/shutdown.sh &> /dev/null 104 sleep 2 105 if ps -ef | grep "java" | grep -w "$CATALINA_HOME" &> /dev/null;then 106 ps -ef | grep "java" | grep -w "$CATALINA_HOME" | awk '{print $2}' | xargs kill -9 &> /dev/null 107 if [ $? -eq 0 ];then 108 echo -e "Stopping tomcat instance $instance_name: \033[32m[ OK ]\033[0m" 109 else 110 echo -e "Stopping tomcat instance $instance_name: \033[31m[FAILED]\033[0m" 111 exit 2 112 fi 113 else 114 echo -e "Stopping tomcat instance $instance_name: \033[32m[ OK ]\033[0m" 115 fi 116 } 117 118 # 重啓tomcat實例 119 restart(){ 120 $CATALINA_HOME/bin/configtest.sh &> /dev/null 121 if [ $? -eq 0 ];then 122 if ps -ef | grep "java" | grep -w "$CATALINA_HOME" &> /dev/null ;then 123 stop 124 else 125 echo -e "Stopping tomcat instance $instance_name: \033[31m[FAILED]\033[0m" 126 fi 127 128 start 129 else 130 exit 2 131 fi 132 } 133 134 # 查看tomcat實例狀態 135 status(){ 136 if ps -ef | grep "java" | grep -w "$CATALINA_HOME" &> /dev/null;then 137 ps -ef | grep "java" | grep -w "$CATALINA_HOME" | awk '{print $2}' | while read java_pid;do 138 echo -e "\033[32mOK: tomcat instance $instance_name is running ($java_pid)\033[0m" 139 done 140 return 0 141 else 142 echo -e "\033[31mWarning: tomcat instance $instance_name is not running\033[0m" 143 return 2 144 fi 145 } 146 147 # tomcat實例配置文件測試 148 configtest(){ 149 $CATALINA_HOME/bin/configtest.sh 150 } 151 152 # 打印tomcat啓動日誌 153 print_log(){ 154 local exit_code 155 whiptail --title "Print Yes/No Log" --yesno "Choose between Yes and No." 10 60 156 exit_code=`echo $?` 157 if [ $exit_code -eq 0 ];then 158 echo -e "\033[32m====================[$instance_name Log]=======================================\033[0m" 159 tailf $CATALINA_HOME/logs/catalina.out 160 else 161 return 1 162 fi 163 }
164 # main 165 # 根據函數syntax肯定tomcat實例的家目錄 166 arg_count=$# 167 arg=("$1" "$2") 168 syntax_check 169 #export CATALINA_HOME=/usr/local/tomcat/instance/$instance_name 170 export CATALINA_HOME=/opt/tomc_instance/$instance_name 171 172 # 173 case $hit_action in 174 start) 175 if ps -ef | grep "java" | grep -w "$CATALINA_HOME" &> /dev/null ;then 176 echo -e "\033[32mNotice: Service tomcat instance $instance_name is running ...\033[0m" 177 exit 1 178 else 179 start 180 sleep 0.3 181 print_log 182 fi 183 ;; 184 185 stop) 186 if ps -ef | grep "java" | grep -w "$CATALINA_HOME" &> /dev/null ;then 187 stop 188 sleep 0.3 189 print_log 190 else 191 echo -e "\033[31mWarning: Service tomcat instance $instance_name is stopped\033[0m" 192 fi 193 ;; 194 195 restart) 196 restart 197 sleep 0.3 198 print_log 199 ;; 200 201 status) 202 status 203 ;; 204 205 configtest) 206 configtest 207 ;; 208 209 print_log) 210 print_log 211 ;; 212 213 *) 214 help_info 215 esac
實例操做:
1.查看一個實例的狀態html
2.查看幫助信息,沒有使用-h選項,不過你能夠故意給錯選項來查看 ,提供了比較簡單地幾個選項。java
3.多實例操做,須要如下的腳本,你能夠把它保存並重名命爲tomcatd,而後把它扔到/etc/init.d/tomcatd ,確保其有執行權限。數組
1 #!/bin/bash 2 # 3 # chkconfig: 2345 99 21 4 # description: tomcat multi instance init scripts 5 6 # 經過instance數組把你要啓動的tomcat實例寫在這裏 7 instance=("tomcat8001" "tomcat8002") 8 9 # 該服務須要用到的執行程序 10 prog=/usr/local/bin/tomcat-multi 11 12 # 經過for循環遍歷數組內的實例名稱啓動多個實例 13 case $1 in 14 start) 15 for i in ${instance[*]};do $prog --instance=$i start;done 16 ;; 17 18 stop) 19 for i in ${instance[*]};do $prog --instance=$i stop;done 20 ;; 21 22 restart) 23 for i in ${instance[*]};do $prog --instance=$i restart;done 24 ;; 25 26 configtest) 27 for i in ${instance[*]};do $prog --instance=$i configtest;done 28 ;; 29 30 status) 31 for i in ${instance[*]};do $prog --instance=$i status;done 32 ;; 33
4. 啓動並打印日誌tomcat
總結:bash
@解決tomcat多實例在部署應用程序後每一個手動重啓。函數
@經過status 選項查看tomcat實例在線狀況測試
@實現tomcat多實例集中管理,固然經過tomcat-multi 能夠實現單實例管理spa
@腳本有不合適地方,還請你們給予指出,這裏只是給你們一個模板rest