最近公司裏面每次上線後端app的load都會狂飆,只好重啓web服務器,以斷開舊的鏈接,好讓新的鏈接可以進來,因此,寫一個重啓的腳原本完成這項任務。php
由於web服務器有兩個類型,nginx和apache,須要設置兩個數組,一個保存全部機器的列表,一個保存nginx的機器列表,在遍歷web服務器列表的同時,判斷是否屬於nginx的列表,若是是執行nginx fast-cgi的重啓,若是不是,重啓apache。nginx
下面是腳本的具體內容:web
- #!/bin/bash
- . common.sh
- ng=( "10.10.3.2" "10.10.3.3" "10.10.3.13" "10.10.3.15" "10.10.3.39" )
- app_restart () {
- for i in "$@"
- do
- echo "${ng[@]}" |grep -wq "$i"
- if [ $? == 0 ];then
- TIME=`date +%F-%T`
- ssh 10.10.3.2 -t "sudo sudo /etc/init.d/php-fpm reload"
- if [ $? -ne 0 ];then
- echo "$TIME $i Failed to start" |tee ./error_list
- fi
- echo "$TIME $i nginx"
- else
- TIME=`date +%F-%T`
- echo "$TIME $i"
- ssh $i -t "sudo /etc/init.d/httpd graceful"
- if [ $? -ne 0 ];then
- echo "$TIME $i Failed to start" |tee ./error_list
- fi
- echo "$TIME $i apache"
- fi
- sleep 10
- # sleep 1
- done
- }
- case "$1" in
- city)
- app_restart ${arrCityAppServer[@]}
- ;;
- my)
- app_restart ${arrMyAppServer[@]}
- ;;
- *)
- echo $"Usage: $0 {city|my}"
- exit 1
- esac