1.此脚本需要nginx安装ginx_upstream_check_module 配置完成平滑重启
2.脚本内容如下:
1 #!/bin/bash 2 cat <<MADAY 3 ===---------------------------------------------------------=== 4 +-------------------------------------------------------------+ 5 A)服务器192.168.1.1 tomcat后台更新 6 B)服务器192.168.1.2 tomcat后台更新 7 C)服务器192.168.1.3 tomcat后台更新 8 D)三台tomcat服务器无人值守后台跟新 9 +-------------------------------------------------------------+ 10 ===---------------------------------------------------------=== 11 MADAY 12 13 export USER=root 14 export PASSWD=Quanjing321 15 #1.##################################### TOMCAT 8080 SHUTDOWN and start script ######################################################## 16 function tomcat1(){ 17 18 cat <<bbb >/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.war 100 sh \$tomcat_8082_start 101 fi 102 103 #=========================================后台启动完成 开盖nginx=============================================================== 104 sleep 5 105 if [ \$tomcat_8080_port_check -eq 1 ];then 106 wget -q -O - http://127.0.0.1:8080/health_check/set/0 > /dev/null 107 echo "tomcat 8080端口启动正常" 108 else 109 echo "tomcat 8080端口启动失败 请检查后台程序" 110 fi 111 sleep 5 112 if [ \$tomcat_8081_port_check -eq 1 ];then 113 wget -q -O - http://127.0.0.1:8081/health_check/set/0 > /dev/null 114 echo "tomcat 8081端口启动正常" 115 else 116 echo "tomcat 8081端口启动失败 请检查后台程序" 117 fi 118 bbb 119 #==================================================脚本发送执行================================================== 120 /usr/bin/expect << EOF 121 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 interact 128 expect eof 129 EOF 130 131 /usr/bin/expect << EOF 132 set timeout -1 133 spawn ssh root@192.168.1.1 134 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 interact 145 expect eof 146 EOF 147 148 } 149 150 151 #2.###############################################tomcat 2 start script############################################### 152 function tomcat2(){ 153 154 cat <<bbb >/root/tomcat_kill 155 #!/bin/bash 156 tomcat_8080_start=/export/servers/tomcat7-1/bin/startup.sh 157 tomcat_8080_dir=/export/servers/tomcat7-1/webapps 158 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.sh 161 timedate=\`date +%Y%m%d%H%M\` 162 163 tomcat_8081_start=/export/servers/tomcat7-2/bin/startup.sh 164 tomcat_8081_dir=/export/servers/tomcat7-2/webapps 165 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.sh 168 169 tomcat_8082_start=/export/servers/tomcat7-3/bin/startup.sh 170 tomcat_8082_dir=/export/servers/tomcat7-3/webapps 171 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.sh 174 #===========================================创建备份war包目录 并且备份上一个版本=============================================== 175 176 if [ -d /export/backup/api ]; then 177 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war 178 else 179 mkdir -p /export/backup/api 180 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war 181 fi 182 #==========================================闭盖nginx 连接不向后台转发========================================================== 183 wget -q -O - http://127.0.0.1:8080/health_check/set/1 > /dev/null 184 sleep 5 185 wget -q -O - http://127.0.0.1:8081/health_check/set/1 > /dev/null 186 sleep 5 187 wget -q -O - http://127.0.0.1:8082/health_check/set/1 > /dev/null 188 sleep 5 189 #==========================================kill进程 启动tomcat================================================================= 190 while true 191 Connect=\`netstat -anpt |grep :8080 |grep ESTABLISHED|wc -l\` 192 do 193 if [ "\$Connect" -lt "1" ]; then 194 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 195 # [[ -n \$tomcat_8080 ]] && kill -9 \$tomcat_8080 196 ps -elf |grep tomcat7-1|grep -v grep |awk '{print \$4}' |xargs kill -9 197 rm -rf \$tomcat_8080_dir/ROOT* 198 cp /root/api.war \$tomcat_8080_dir/ROOT.war 199 sh \$tomcat_8080_start 200 sleep 10 201 break 202 else 203 echo "Exist process" 204 sleep 2 205 fi 206 done 207 208 while true 209 Connect=\`netstat -anpt |grep :8081 |grep ESTABLISHED|wc -l\` 210 do 211 if [ "\$Connect" -lt "1" ]; then 212 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 213 # [[ -n \$tomcat_8081 ]] && kill -9 \$tomcat_8081 214 ps -elf |grep tomcat7-2|grep -v grep |awk '{print \$4}' |xargs kill -9 215 rm -rf \$tomcat_8081_dir/ROOT* 216 cp /root/api.war \$tomcat_8081_dir/ROOT.war 217 sh \$tomcat_8081_start 218 sleep 10 219 break 220 else 221 echo "Exist process" 222 sleep 2 223 fi 224 done 225 226 while true 227 Connect=\`netstat -anpt |grep :8082 |grep ESTABLISHED|wc -l\` 228 do 229 if [ "\$Connect" -lt "1" ]; then 230 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 231 # [[ -n \$tomcat_8082 ]] && kill -9 \$tomcat_8082 232 ps -elf |grep tomcat7-3|grep -v grep |awk '{print \$4}' |xargs kill -9 233 rm -rf \$tomcat_8082_dir/ROOT* 234 cp /root/api.war \$tomcat_8082_dir/ROOT.war 235 sh \$tomcat_8082_start 236 sleep 10 237 break 238 else 239 echo "Exist process" 240 sleep 2 241 fi 242 done 243 #=========================================后台启动完成 开盖nginx=============================================================== 244 sleep 5 245 if [ \$tomcat_8080_port_check -eq 1 ];then 246 wget -q -O - http://127.0.0.1:8080/health_check/set/0 > /dev/null 247 echo "tomcat 8080端口启动正常" 248 else 249 echo "tomcat 8080端口启动失败 请检查后台程序" 250 fi 251 sleep 5 252 if [ \$tomcat_8081_port_check -eq 1 ];then 253 wget -q -O - http://127.0.0.1:8081/health_check/set/0 > /dev/null 254 echo "tomcat 8081端口启动正常" 255 else 256 echo "tomcat 8081端口启动失败 请检查后台程序" 257 fi 258 sleep 5 259 if [ \$tomcat_8082_port_check -eq 1 ];then 260 wget -q -O - http://127.0.0.1:8082/health_check/set/0 > /dev/null 261 echo "tomcat 8082端口启动正常" 262 else 263 echo "tomcat 8082端口启动失败 请检查后台程序" 264 fi 265 bbb 266 ################################################脚本发送执行############################################### 267 /usr/bin/expect << EOF 268 set timeout -1 269 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 interact 275 expect eof 276 EOF 277 278 /usr/bin/expect << EOF 279 set timeout -1 280 spawn ssh root@192.168.1.2 281 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 interact 292 expect eof 293 exit 294 EOF 295 } 296 297 #3.###############################################tomcat 3 start script############################################### 298 function tomcat3(){ 299 300 cat <<bbb >/root/tomcat_kill 301 #!/bin/bash 302 tomcat_8080_start=/export/servers/tomcat7-1/bin/startup.sh 303 tomcat_8080_dir=/export/servers/tomcat7-1/webapps 304 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.sh 307 timedate=\`date +%Y%m%d%H%M\` 308 309 tomcat_8081_start=/export/servers/tomcat7-2/bin/startup.sh 310 tomcat_8081_dir=/export/servers/tomcat7-2/webapps 311 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.sh 314 315 tomcat_8082_start=/export/servers/tomcat7-3/bin/startup.sh 316 tomcat_8082_dir=/export/servers/tomcat7-3/webapps 317 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.sh 320 #===========================================创建备份war包目录 并且备份上一个版本=============================================== 321 322 if [ -d /export/backup/api ]; then 323 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war 324 else 325 mkdir -p /export/backup/api 326 cp /export/servers/tomcat7-3/webapps/ROOT.war /export/backup/api/api_\${timedate}.war 327 fi 328 329 #==========================================闭盖nginx 连接不向后台转发========================================================== 330 wget -q -O - http://127.0.0.1:8080/health_check/set/1 > /dev/null 331 sleep 5 332 wget -q -O - http://127.0.0.1:8081/health_check/set/1 > /dev/null 333 sleep 5 334 #==========================================kill进程 启动tomcat================================================================= 335 while true 336 Connect=\`netstat -anpt |grep :8080 |grep ESTABLISHED|wc -l\` 337 do 338 if [ "\$Connect" -lt "1" ]; then 339 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 340 # [[ -n \$tomcat_8080 ]] && kill -9 \$tomcat_8080 341 ps -elf |grep tomcat7-1|grep -v grep |awk '{print \$4}' |xargs kill -9 342 rm -rf \$tomcat_8080_dir/ROOT* 343 cp /root/api.war \$tomcat_8080_dir/ROOT.war 344 sh \$tomcat_8080_start 345 break 346 else 347 echo "Exist process" 348 sleep 2 349 fi 350 done 351 352 while true 353 Connect=\`netstat -anpt |grep :8081 |grep ESTABLISHED|wc -l\` 354 do 355 if [ "\$Connect" -lt "1" ]; then 356 echo "当前服务器没有ESTABLISHED连接停止tomcat"; 357 # [[ -n \$tomcat_8081 ]] && kill -9 \$tomcat_8081 358 ps -elf |grep tomcat7-2|grep -v grep |awk '{print \$4}' |xargs kill -9 359 rm -rf \$tomcat_8081_dir/ROOT* 360 cp /root/api.war \$tomcat_8081_dir/ROOT.war 361 sh \$tomcat_8081_start 362 sleep 10 363 break 364 else 365 echo "Exist process" 366 sleep 2 367 fi 368 done 369 sleep 5 370 371 if [ "\$tomcat_8082_port_check" -eq "1" ]; then 372 ps -elf |grep tomcat7-3|grep -v grep |awk '{print \$4}' |xargs kill -9 373 rm -rf \$tomcat_8082_dir/ROOT* 374 cp /root/api.war \$tomcat_8082_dir/ROOT.war 375 sh \$tomcat_8082_start 376 elif [ "\$tomcat_8082_port_check" -eq "1" ];then 377 echo "tomcat 8082 启动成功" 378 else 379 rm -rf \$tomcat_8082_dir/ROOT* 380 cp /root/api.war \$tomcat_8082_dir/ROOT.war 381 sh \$tomcat_8082_start 382 fi 383 384 #=========================================后台启动完成 开盖nginx=============================================================== 385 sleep 5 386 if [ \$tomcat_8080_port_check -eq 1 ];then 387 wget -q -O - http://127.0.0.1:8080/health_check/set/0 > /dev/null 388 echo "tomcat 8080端口启动正常" 389 else 390 echo "tomcat 8080端口启动失败 请检查后台程序" 391 fi 392 sleep 5 393 if [ \$tomcat_8081_port_check -eq 1 ];then 394 wget -q -O - http://127.0.0.1:8081/health_check/set/0 > /dev/null 395 echo "tomcat 8081端口启动正常" 396 else 397 echo "tomcat 8081端口启动失败 请检查后台程序" 398 fi 399 bbb 400 #==================================================脚本发送执行================================================== 401 /usr/bin/expect << EOF 402 set timeout -1 403 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 interact 409 expect eof 410 EOF 411 412 /usr/bin/expect << EOF 413 set timeout -1 414 spawn ssh root@192.168.1.3 415 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 interact 426 expect eof 427 EOF 428 } 429 ##############################################菜单目录############################################################# 430 read -p "请您输入(A|B|C|D): " zm 431 case "$zm" in 432 A|a) 433 tomcat1 434 ;; 435 B|b) 436 tomcat2 437 ;; 438 C|c) 439 tomcat3 440 ;; 441 D|d) 442 tomcat1 443 tomcat2 444 tomcat3 445 ;; 446 esac 447 exit 0
来源:https://www.cnblogs.com/wangshaojie/p/7132801.html