1.此脚本需要nginx安装ginx_upstream_check_module 配置完成平滑重启
2.脚本内容如下:
1 #!/bin/bash 2 cat </root/tomcat_kill 19 #!/bin/bash 20 tomcat_8080_start=/export/servers/tomcat7-1/bin/startup.sh 21 tomcat_8080_dir=/export/servers/tomcat7-1/webapps 22 tomcat_8080_port_check=\`netstat -anpt |grep LISTEN|grep :8080|wc -l\` 23 tomcat_8080=\`netstat -anpt |grep LISTEN|grep :8080| awk '{print \$7}' |awk -F "/" '{print \$1}'\` 24 tomcat_8080_shutdown_dir=/export/servers/tomcat7-1/bin/shutdown.sh 25 timedate=\`date +%Y%m%d%H%M\` 26 27 tomcat_8081_start=/export/servers/tomcat7-2/bin/startup.sh 28 tomcat_8081_dir=/export/servers/tomcat7-2/webapps 29 tomcat_8081_port_check=\`netstat -anpt |grep LISTEN|grep :8081|wc -l\` 30 tomcat_8081=\`netstat -anpt |grep LISTEN|grep :8081| awk '{print \$7}' |awk -F "/" '{print \$1}'\` 31 tomcat_8081_shutdown_dir=/export/servers/tomcat7-2/bin/shutdown.sh 32 33 tomcat_8082_start=/export/servers/tomcat7-3/bin/startup.sh 34 tomcat_8082_dir=/export/servers/tomcat7-3/webapps 35 tomcat_8082_port_check=\`netstat -anpt |grep LISTEN|grep :8082|wc -l\` 36 tomcat_8082=\`netstat -anpt |grep LISTEN|grep :8082| awk '{print \$7}' |awk -F "/" '{print \$1}'\` 37 tomcat_8082_shutdown_dir=/export/servers/tomcat7-3/bin/shutdown.sh 38 #===========================================创建备份war包目录 并且备份上一个版本=============================================== 39 40 if [ -d /export/backup/api ]; then 41 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war 42 else 43 mkdir -p /export/backup/api 44 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war 45 fi 46 47 #==========================================闭盖nginx 连接不向后台转发========================================================== 48 wget -q -O - http://127.0.0.1:8080/health_check/set/1 > /dev/null 49 sleep 5 50 wget -q -O - http://127.0.0.1:8081/health_check/set/1 > /dev/null 51 sleep 5 52 #==========================================kill进程 启动tomcat================================================================= 53 while true 54 Connect=\`netstat -anpt |grep :8080 |grep ESTABLISHED|wc -l\` 55 do 56 if [ "\$Connect" -lt "1" ]; then 57 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 58 # [[ -n \$tomcat_8080 ]] && kill -9 \$tomcat_8080 59 ps -elf |grep tomcat7-1|grep -v grep |awk '{print \$4}' |xargs kill -9 60 rm -rf \$tomcat_8080_dir/ROOT* 61 cp /root/api.war \$tomcat_8080_dir/ROOT.war 62 sh \$tomcat_8080_start 63 sleep 10 64 break 65 else 66 echo "Exist process" 67 sleep 2 68 fi 69 done 70 71 while true 72 Connect=\`netstat -anpt |grep :8081 |grep ESTABLISHED|wc -l\` 73 do 74 if [ "\$Connect" -lt "1" ]; then 75 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 76 # [[ -n \$tomcat_8081 ]] && kill -9 \$tomcat_8081 77 ps -elf |grep tomcat7-2|grep -v grep |awk '{print \$4}' |xargs kill -9 78 rm -rf \$tomcat_8081_dir/ROOT* 79 cp /root/api.war \$tomcat_8081_dir/ROOT.war 80 sh \$tomcat_8081_start 81 sleep 10 82 break 83 else 84 echo "Exist process" 85 sleep 2 86 fi 87 done 88 sleep 5 89 90 if [ "\$tomcat_8082_port_check" -eq "1" ]; then 91 ps -elf |grep tomcat7-3|grep -v grep |awk '{print \$4}' |xargs kill -9 92 rm -rf \$tomcat_8082_dir/ROOT* 93 cp /root/api.war \$tomcat_8082_dir/ROOT.war 94 sh \$tomcat_8082_start 95 elif [ "\$tomcat_8082_port_check" -eq "1" ];then 96 echo "tomcat 8082 启动成功" 97 else 98 rm -rf \$tomcat_8082_dir/ROOT* 99 cp /root/api.war \$tomcat_8082_dir/ROOT.war100 sh \$tomcat_8082_start101 fi102 103 #=========================================后台启动完成 开盖nginx===============================================================104 sleep 5105 if [ \$tomcat_8080_port_check -eq 1 ];then106 wget -q -O - http://127.0.0.1:8080/health_check/set/0 > /dev/null107 echo "tomcat 8080端口启动正常"108 else109 echo "tomcat 8080端口启动失败 请检查后台程序"110 fi111 sleep 5112 if [ \$tomcat_8081_port_check -eq 1 ];then113 wget -q -O - http://127.0.0.1:8081/health_check/set/0 > /dev/null114 echo "tomcat 8081端口启动正常"115 else116 echo "tomcat 8081端口启动失败 请检查后台程序"117 fi118 bbb119 #==================================================脚本发送执行==================================================120 /usr/bin/expect << EOF121 set timeout -1 122 spawn scp /root/tomcat_kill /export/shell/appdeploy/api.war root@192.168.1.1:/root/123 expect {124 "(yes/no)?" {send "yes\r";exp_continue}125 "password:" {send "$PASSWD\r"}126 }127 interact128 expect eof129 EOF130 131 /usr/bin/expect << EOF132 set timeout -1133 spawn ssh root@192.168.1.1134 expect {135 "(yes/no)?" {send "yes\r";exp_continue}136 "password:" {send "$PASSWD\r"}137 }138 expect -re "\](\$|#)"139 send "sh -x /root/tomcat_kill \r" 140 expect -re "\](\$|#)"141 send "rm -rf /root/tomcat_kill \r"142 expect -re "\](\$|#)"143 send "exit\r"144 interact145 expect eof146 EOF147 148 }149 150 151 #2.###############################################tomcat 2 start script###############################################152 function tomcat2(){153 154 cat < /root/tomcat_kill155 #!/bin/bash156 tomcat_8080_start=/export/servers/tomcat7-1/bin/startup.sh157 tomcat_8080_dir=/export/servers/tomcat7-1/webapps158 tomcat_8080_port_check=\`netstat -anpt |grep LISTEN|grep :8080|wc -l\`159 tomcat_8080=\`netstat -anpt |grep LISTEN|grep :8080| awk '{print \$7}' |awk -F "/" '{print \$1}'\`160 tomcat_8080_shutdown_dir=/export/servers/tomcat7-1/bin/shutdown.sh161 timedate=\`date +%Y%m%d%H%M\`162 163 tomcat_8081_start=/export/servers/tomcat7-2/bin/startup.sh164 tomcat_8081_dir=/export/servers/tomcat7-2/webapps165 tomcat_8081_port_check=\`netstat -anpt |grep LISTEN|grep :8081|wc -l\`166 tomcat_8081=\`netstat -anpt |grep LISTEN|grep :8081| awk '{print \$7}' |awk -F "/" '{print \$1}'\`167 tomcat_8081_shutdown_dir=/export/servers/tomcat7-2/bin/shutdown.sh168 169 tomcat_8082_start=/export/servers/tomcat7-3/bin/startup.sh170 tomcat_8082_dir=/export/servers/tomcat7-3/webapps171 tomcat_8082_port_check=\`netstat -anpt |grep LISTEN|grep :8082|wc -l\`172 tomcat_8082=\`netstat -anpt |grep LISTEN|grep :8082| awk '{print \$7}' |awk -F "/" '{print \$1}'\`173 tomcat_8082_shutdown_dir=/export/servers/tomcat7-3/bin/shutdown.sh174 #===========================================创建备份war包目录 并且备份上一个版本===============================================175 176 if [ -d /export/backup/api ]; then177 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war178 else179 mkdir -p /export/backup/api180 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war181 fi182 #==========================================闭盖nginx 连接不向后台转发==========================================================183 wget -q -O - http://127.0.0.1:8080/health_check/set/1 > /dev/null184 sleep 5185 wget -q -O - http://127.0.0.1:8081/health_check/set/1 > /dev/null186 sleep 5187 wget -q -O - http://127.0.0.1:8082/health_check/set/1 > /dev/null188 sleep 5189 #==========================================kill进程 启动tomcat================================================================= 190 while true191 Connect=\`netstat -anpt |grep :8080 |grep ESTABLISHED|wc -l\`192 do193 if [ "\$Connect" -lt "1" ]; then194 echo "当前服务器没有ESTABLISHED连接停止tomcat";195 # [[ -n \$tomcat_8080 ]] && kill -9 \$tomcat_8080196 ps -elf |grep tomcat7-1|grep -v grep |awk '{print \$4}' |xargs kill -9197 rm -rf \$tomcat_8080_dir/ROOT*198 cp /root/api.war \$tomcat_8080_dir/ROOT.war199 sh \$tomcat_8080_start200 sleep 10201 break202 else203 echo "Exist process"204 sleep 2205 fi206 done207 208 while true209 Connect=\`netstat -anpt |grep :8081 |grep ESTABLISHED|wc -l\`210 do211 if [ "\$Connect" -lt "1" ]; then212 echo "当前服务器没有ESTABLISHED连接停止tomcat";213 # [[ -n \$tomcat_8081 ]] && kill -9 \$tomcat_8081214 ps -elf |grep tomcat7-2|grep -v grep |awk '{print \$4}' |xargs kill -9215 rm -rf \$tomcat_8081_dir/ROOT*216 cp /root/api.war \$tomcat_8081_dir/ROOT.war217 sh \$tomcat_8081_start218 sleep 10219 break220 else221 echo "Exist process"222 sleep 2223 fi224 done225 226 while true227 Connect=\`netstat -anpt |grep :8082 |grep ESTABLISHED|wc -l\`228 do229 if [ "\$Connect" -lt "1" ]; then230 echo "当前服务器没有ESTABLISHED连接停止tomcat";231 # [[ -n \$tomcat_8082 ]] && kill -9 \$tomcat_8082232 ps -elf |grep tomcat7-3|grep -v grep |awk '{print \$4}' |xargs kill -9233 rm -rf \$tomcat_8082_dir/ROOT*234 cp /root/api.war \$tomcat_8082_dir/ROOT.war235 sh \$tomcat_8082_start236 sleep 10237 break238 else239 echo "Exist process"240 sleep 2241 fi242 done243 #=========================================后台启动完成 开盖nginx===============================================================244 sleep 5245 if [ \$tomcat_8080_port_check -eq 1 ];then246 wget -q -O - http://127.0.0.1:8080/health_check/set/0 > /dev/null247 echo "tomcat 8080端口启动正常"248 else249 echo "tomcat 8080端口启动失败 请检查后台程序"250 fi251 sleep 5252 if [ \$tomcat_8081_port_check -eq 1 ];then253 wget -q -O - http://127.0.0.1:8081/health_check/set/0 > /dev/null254 echo "tomcat 8081端口启动正常"255 else256 echo "tomcat 8081端口启动失败 请检查后台程序"257 fi258 sleep 5259 if [ \$tomcat_8082_port_check -eq 1 ];then260 wget -q -O - http://127.0.0.1:8082/health_check/set/0 > /dev/null261 echo "tomcat 8082端口启动正常"262 else263 echo "tomcat 8082端口启动失败 请检查后台程序"264 fi265 bbb266 ################################################脚本发送执行###############################################267 /usr/bin/expect << EOF 268 set timeout -1269 spawn scp /root/tomcat_kill /export/shell/appdeploy/api.war root@192.168.1.2:/root/270 expect {271 "(yes/no)?" {send "yes\r";exp_continue}272 "password:" {send "$PASSWD\r"}273 }274 interact275 expect eof276 EOF277 278 /usr/bin/expect << EOF279 set timeout -1280 spawn ssh root@192.168.1.2281 expect {282 "(yes/no)?" {send "yes\r";exp_continue}283 "password:" {send "$PASSWD\r"}284 }285 expect -re "\](\$|#)"286 send "sh -x /root/tomcat_kill \r" 287 expect -re "\](\$|#)"288 send "rm -rf /root/tomcat_kill \r"289 expect -re "\](\$|#)"290 send "exit\r"291 interact292 expect eof293 exit294 EOF295 }296 297 #3.###############################################tomcat 3 start script###############################################298 function tomcat3(){299 300 cat < /root/tomcat_kill301 #!/bin/bash302 tomcat_8080_start=/export/servers/tomcat7-1/bin/startup.sh303 tomcat_8080_dir=/export/servers/tomcat7-1/webapps304 tomcat_8080_port_check=\`netstat -anpt |grep LISTEN|grep :8080|wc -l\`305 tomcat_8080=\`netstat -anpt |grep LISTEN|grep :8080| awk '{print \$7}' |awk -F "/" '{print \$1}'\`306 tomcat_8080_shutdown_dir=/export/servers/tomcat7-1/bin/shutdown.sh307 timedate=\`date +%Y%m%d%H%M\`308 309 tomcat_8081_start=/export/servers/tomcat7-2/bin/startup.sh310 tomcat_8081_dir=/export/servers/tomcat7-2/webapps311 tomcat_8081_port_check=\`netstat -anpt |grep LISTEN|grep :8081|wc -l\`312 tomcat_8081=\`netstat -anpt |grep LISTEN|grep :8081| awk '{print \$7}' |awk -F "/" '{print \$1}'\`313 tomcat_8081_shutdown_dir=/export/servers/tomcat7-2/bin/shutdown.sh314 315 tomcat_8082_start=/export/servers/tomcat7-3/bin/startup.sh316 tomcat_8082_dir=/export/servers/tomcat7-3/webapps317 tomcat_8082_port_check=\`netstat -anpt |grep LISTEN|grep :8082|wc -l\`318 tomcat_8082=\`netstat -anpt |grep LISTEN|grep :8082| awk '{print \$7}' |awk -F "/" '{print \$1}'\`319 tomcat_8082_shutdown_dir=/export/servers/tomcat7-3/bin/shutdown.sh320 #===========================================创建备份war包目录 并且备份上一个版本===============================================321 322 if [ -d /export/backup/api ]; then323 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war324 else325 mkdir -p /export/backup/api326 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war327 fi328 329 #==========================================闭盖nginx 连接不向后台转发==========================================================330 wget -q -O - http://127.0.0.1:8080/health_check/set/1 > /dev/null331 sleep 5332 wget -q -O - http://127.0.0.1:8081/health_check/set/1 > /dev/null333 sleep 5334 #==========================================kill进程 启动tomcat================================================================= 335 while true336 Connect=\`netstat -anpt |grep :8080 |grep ESTABLISHED|wc -l\`337 do338 if [ "\$Connect" -lt "1" ]; then339 echo "当前服务器没有ESTABLISHED连接停止tomcat";340 # [[ -n \$tomcat_8080 ]] && kill -9 \$tomcat_8080341 ps -elf |grep tomcat7-1|grep -v grep |awk '{print \$4}' |xargs kill -9342 rm -rf \$tomcat_8080_dir/ROOT*343 cp /root/api.war \$tomcat_8080_dir/ROOT.war344 sh \$tomcat_8080_start345 break346 else347 echo "Exist process"348 sleep 2349 fi350 done351 352 while true353 Connect=\`netstat -anpt |grep :8081 |grep ESTABLISHED|wc -l\`354 do355 if [ "\$Connect" -lt "1" ]; then356 echo "当前服务器没有ESTABLISHED连接停止tomcat";357 # [[ -n \$tomcat_8081 ]] && kill -9 \$tomcat_8081358 ps -elf |grep tomcat7-2|grep -v grep |awk '{print \$4}' |xargs kill -9359 rm -rf \$tomcat_8081_dir/ROOT*360 cp /root/api.war \$tomcat_8081_dir/ROOT.war361 sh \$tomcat_8081_start362 sleep 10363 break364 else365 echo "Exist process"366 sleep 2367 fi368 done369 sleep 5370 371 if [ "\$tomcat_8082_port_check" -eq "1" ]; then372 ps -elf |grep tomcat7-3|grep -v grep |awk '{print \$4}' |xargs kill -9373 rm -rf \$tomcat_8082_dir/ROOT*374 cp /root/api.war \$tomcat_8082_dir/ROOT.war375 sh \$tomcat_8082_start376 elif [ "\$tomcat_8082_port_check" -eq "1" ];then377 echo "tomcat 8082 启动成功"378 else379 rm -rf \$tomcat_8082_dir/ROOT*380 cp /root/api.war \$tomcat_8082_dir/ROOT.war381 sh \$tomcat_8082_start382 fi383 384 #=========================================后台启动完成 开盖nginx===============================================================385 sleep 5386 if [ \$tomcat_8080_port_check -eq 1 ];then387 wget -q -O - http://127.0.0.1:8080/health_check/set/0 > /dev/null388 echo "tomcat 8080端口启动正常"389 else390 echo "tomcat 8080端口启动失败 请检查后台程序"391 fi392 sleep 5393 if [ \$tomcat_8081_port_check -eq 1 ];then394 wget -q -O - http://127.0.0.1:8081/health_check/set/0 > /dev/null395 echo "tomcat 8081端口启动正常"396 else397 echo "tomcat 8081端口启动失败 请检查后台程序"398 fi399 bbb400 #==================================================脚本发送执行==================================================401 /usr/bin/expect << EOF 402 set timeout -1403 spawn scp /root/tomcat_kill /export/shell/appdeploy/api.war root@192.168.1.3:/root/404 expect {405 "(yes/no)?" {send "yes\r";exp_continue}406 "password:" {send "$PASSWD\r"}407 }408 interact409 expect eof410 EOF411 412 /usr/bin/expect << EOF413 set timeout -1414 spawn ssh root@192.168.1.3415 expect {416 "(yes/no)?" {send "yes\r";exp_continue}417 "password:" {send "$PASSWD\r"}418 }419 expect -re "\](\$|#)"420 send "sh -x /root/tomcat_kill \r" 421 expect -re "\](\$|#)"422 send "rm -rf /root/tomcat_kill \r"423 expect -re "\](\$|#)"424 send "exit\r"425 interact426 expect eof427 EOF428 }429 ##############################################菜单目录#############################################################430 read -p "请您输入(A|B|C|D): " zm431 case "$zm" in432 A|a)433 tomcat1434 ;; 435 B|b) 436 tomcat2437 ;; 438 C|c) 439 tomcat3440 ;;441 D|d)442 tomcat1 443 tomcat2444 tomcat3445 ;; 446 esac447 exit 0