1 #!/bin/sh 2 # 3 # function: Tomcat mutil instance init script for Linux. 4 # author: 5 # QQ:765482322 6 # mail: [email protected] 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 EOF 42 } 43 44 # 根据给定的参数判断用户对tomcat实例所要执行的动作 45 action(){ 46 run_action=("start" "stop" "restart" "configtest" "status") 47 local i=1 48 while [ $i -le 2 ];do 49 for j in ${run_action[*]};do 50 if [ ${arg[$i-1]} == $j ];then 51 hit_action=$j;break 52 else 53 continue 54 fi 55 done 56 if [ -n "$hit_action" ];then 57 #echo "hit_action=$hit_action" 58 break 59 else 60 let i++ 61 fi 62 done 63 if [ -z "$hit_action" ];then 64 help_info 65 exit 1 66 fi 67 68 } 69 70 # 通过选项--instance 得到tomcat实例所在路径 71 option() { 72 local i=1 73 while [ $i -le 2 ];do 74 if echo ${arg[$i-1]} | egrep -i "(^--instance=)" &> /dev/null;then 75 instance_name=$(echo ${arg[$i-1]} | sed -r ‘s/--instance=(.*)/\1/‘) 76 break 77 else 78 let i++ 79 fi 80 done 81 if [ -z "$instance_name" ];then 82 help_info 83 exit 1 84 fi 85 } 86 87 # 启动tomcat实例 88 start(){ 89 $CATALINA_HOME/bin/startup.sh &> /dev/null 90 sleep 2 91 if ps -ef | grep "java" | grep "$CATALINA_HOME" &> /dev/null ;then 92 echo -e "Starting tomcat instance $instance_name: \033[32m[ OK ]\033[0m" 93 # echo $tomcat_getpid | bash > $pidfile 94 else 95 echo -e "Starting tomcat instance $instance_name: \033[31m[FAILED]\033[0m" 96 exit 1 97 fi 98 } 99 100 # 停止tomcat实例,注意有时使用catalina.sh 执行 stop不成功,所以加了层判断,如果使用catalina.sh 关闭不了进程,就使用kill杀死进程 101 stop(){ 102 $CATALINA_HOME/bin/shutdown.sh &> /dev/null 103 sleep 2 104 if ps -ef | grep "java" | grep "$CATALINA_HOME" &> /dev/null;then 105 ps -ef | grep "java" | grep "$CATALINA_HOME" | awk ‘{print $2}‘ | xargs kill -9 &> /dev/null 106 if [ $? -eq 0 ];then 107 echo -e "Stopping tomcat instance $instance_name: \033[32m[ OK ]\033[0m" 108 else 109 echo -e "Stopping tomcat instance $instance_name: \033[31m[FAILED]\033[0m" 110 exit 2 111 fi 112 else 113 echo -e "Stopping tomcat instance $instance_name: \033[32m[ OK ]\033[0m" 114 fi 115 } 116 117 # 重启tomcat实例 118 restart(){ 119 $CATALINA_HOME/bin/configtest.sh &> /dev/null 120 if [ $? -eq 0 ];then 121 if ps -ef | grep "java" | grep "$CATALINA_HOME" &> /dev/null ;then 122 stop 123 else 124 echo -e "Stopping tomcat instance $instance_name: \033[31m[FAILED]\033[0m" 125 fi 126 127 start 128 else 129 exit 2 130 fi 131 } 132 133 # 查看tomcat实例状态 134 status(){ 135 if ps -ef | grep "java" | grep "$CATALINA_HOME" &> /dev/null;then 136 ps -ef | grep "java" | grep "$CATALINA_HOME" | awk ‘{print $2}‘ | while read java_pid;do 137 echo -e "\033[32mOK: tomcat instance $instance_name is running ($java_pid)\033[0m" 138 done 139 return 0 140 else 141 echo -e "\033[31mWarning: tomcat instance $instance_name is not running\033[0m" 142 return 2 143 fi 144 } 145 146 # tomcat实例配置文件测试 147 configtest(){ 148 $CATALINA_HOME/bin/configtest.sh 149 } 150 151 152 # main 153 # 根据函数syntax确定tomcat实例的家目录 154 arg_count=$# 155 arg=("$1" "$2") 156 syntax_check 157 export CATALINA_HOME=/usr/local/tomcat/instance/$instance_name 158 159 # 160 case $hit_action in 161 start) 162 if ps -ef | grep "java" | grep "$CATALINA_HOME" &> /dev/null ;then 163 echo -e "\033[32mNotice: Service tomcat instance $instance_name is running ...\033[0m" 164 exit 1 165 else 166 start 167 fi 168 ;; 169 170 stop) 171 if ps -ef | grep "java" | grep "$CATALINA_HOME" &> /dev/null ;then 172 stop 173 else 174 echo -e "\033[31mWarning: Service tomcat instance $instance_name is stopped\033[0m" 175 fi 176 ;; 177 178 restart) 179 restart 180 ;; 181 182 status) 183 status 184 ;; 185 186 configtest) 187 configtest 188 ;; 189 190 *) 191 help_info 192 esac
实例操作:
1.查看一个实例的状态
2.查看帮助信息,没有使用-h选项,不过你可以故意给错选项来查看 ,提供了比较简单地几个选项。
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
总结:
@解决tomcat多实例在部署应用程序后每个手动重启。
@通过status 选项查看tomcat实例在线情况
@实现tomcat多实例集中管理,当然通过tomcat-multi 可以实现单实例管理
@脚本有不合适地方,还请大家给予指出,这里只是给大家一个模板
时间: 2024-10-01 03:07:50