您好,登錄后才能下訂單哦!
由于需要對(duì)多個(gè)項(xiàng)目的多個(gè)應(yīng)用進(jìn)行管理,項(xiàng)目路徑已經(jīng)做了標(biāo)準(zhǔn)化部署,所以需要一個(gè)統(tǒng)一的腳本來(lái)管理所有的啟停操作。
里面附帶日志、進(jìn)程啟動(dòng)檢查、批量狀態(tài)查看等功能。
以下為腳本相關(guān)內(nèi)容,有些寫(xiě)法有點(diǎn)low,暫時(shí)沒(méi)有想到更好的寫(xiě)法,有興趣的可以留言
#!/bin/bash #用途:本腳本用來(lái)管理tomcat節(jié)點(diǎn)的啟動(dòng)、停止、重啟,以及查看tomcat節(jié)點(diǎn)的狀態(tài) #作者:張曉龍 #版本v1.0 #定義幫助說(shuō)明函數(shù),語(yǔ)法錯(cuò)誤時(shí)返回使用說(shuō)明 template(){ echo "Usage: $0 -n appname -i node action" echo "-n appname:specifies the application name" echo "-i node:specifie node number of application,it must followed by the -n option." echo "If the node is all,all nodes of application will be operated. " echo "action start/restart/stop:start or stop or restart the specified application node" exit 101 } #使用getopt獲取應(yīng)用名稱、節(jié)點(diǎn)數(shù),節(jié)點(diǎn)數(shù)可以為所有 RST=$(getopt -o n:i: -- "$@") if [ $? -ne 0 ];then template fi eval set -- "$RST" while [ -n "$1" ] do case "$1" in -n) if [[ "$2" =~ -. ]];then #語(yǔ)法錯(cuò)誤 template; fi if [[ "$3" =~ -. ]];then APPNAME="$2"; fi if [[ $(ps aux | grep java|grep ${APPNAME}|wc -l) == 0 ]];then template; shift 2 fi;; -i) if [[ "$2" =~ -. ]];then#語(yǔ)法錯(cuò)誤 template fi if [[ "$3" =~ -. ]];then NODE="$2"; #判斷輸入的節(jié)點(diǎn),如果不是數(shù)字或者all,則進(jìn)行報(bào)錯(cuò)并退出 if [[ $(echo ${NODE}|grep -E '^[0-9]+$|all') == "" ]];then echo "Error type!Argment with $1 must be init or all!" template fi shift 2 fi;; --) shift; break;; *) template break;; esac done #全局初始化變量定義 init_args(){ export DAY=$(date +%Y-%m-%d) export TIME=$(date +%H-%M-%S) export ALIVE=$(echo -e "\e[1;32m[ALIVE]\e[0m") export NULL=$(echo -e "\e[1;31m[N/A]\e[0m") export ERROR=$(echo -e "\e[1;31m[ERROR]\e[0m") export WARNING=$(echo -e "\e[1;33m[WARNING]\e[0m") [ ! -d ${HOME}/scripts/logs ];mkdir -p ${HOME}/scripts/logs [ ! -d ${HOME}/scripts/logs/tomcat.log ];touch ${HOME}/scripts/logs/tomcat.log export LOG_FILE=${HOME}/scripts/logs/tomcat.log } #日志模塊 log(){ DATE=$(date +"[%Y-%m-%d %H:%M:%S]") echo ${DATE} $* echo ${DATE} $* >> ${LOG_FILE} } #tomcat啟動(dòng)定時(shí)器,低于60秒繼續(xù)執(zhí)行腳本,高于記錄報(bào)錯(cuò)信息 timeout_start() { WAITFOR=60 COMMAND=$* ${COMMAND} & COMMANDPID=$! ( sleep ${WAITFOR} ; echo '1' >temp.log && kill -9 ${COMMANDPID} >/dev/null 2>&1 ) & SLEEPPID=${PPID} wait ${COMMANDPID} >/dev/denull 2>&1 kill ${SLEEPPID} > /dev/null 2>&1 } check_err(){ if [[ $(cat temp.log) == 1 ]];then rm temp.log log $* fi } start(){ WORK_DIR=${HOME}/${APPNAME} if [[ "${NODE}" == all ]];then #檢查是否有進(jìn)程存活,如果有,做出判斷,是殺掉后再啟動(dòng),還是保留,只啟動(dòng)未啟動(dòng)的 NODE_NUMS=$(ls ${WORK_DIR}|grep tomcat_${APPNAME}.*|wc -l) for i in $(seq 1 ${NODE_NUMS}) do NODE_PID=$(ps aux | grep java|grep tomcat_${APPNAME}_$i|grep -v grep|awk '{print $2}') if [[ $(echo ${#NODE_PID}) != 0 ]];then read -p "${WARNING} Process ${APPNAME} node $i is running now!Do you want to restart it?(Y|N)" input case $input in Y|y) log " stop ${APPNAME} node is $i pid is ${NODE_PID}}" kill -9 ${NODE_PID};; n|N) echo "${WARNING}: ${APPNAME} node $i pid ${NODE_PID} is already start!" continue;; esac else cd ${WORK_DIR}/tomcat_${APPNAME}_$i/bin log " start ${APPNAME} node is $i pid is ${NODE_PID}" sh startup.sh > /dev/null 2>&1 timeout_start tail -f ../logs/catalina.${DAY}.log | while read line do TAIL_PID=$(ps aux | grep "tail -f ../logs/catalina.${DAY}.log"|awk '{print $2}') if [[ $line =~ .*Server\ startup\ in.* ]];then kill -9 ${TAIL_PID} fi done check_err " ${ERROR} ${APPNAME} node $i start timeout!" fi done #如果指定了節(jié)點(diǎn),那么就判斷節(jié)點(diǎn)是否是啟動(dòng)狀態(tài),如果啟動(dòng),殺掉后再啟動(dòng) elif [[ ${NODE} != all ]];then NODE_PID=$(ps aux | grep tomcat_${APPNAME}_${NODE}|grep -v grep|awk '{print $2}') if [[ $(echo ${#NODE_PID}) != 0 ]];then read -p "${WARNING} Process ${APPNAME} node ${NODE_PID} is running now!Do you want to restart it?(Y|N)" input case $input in Y|y) log " stop ${APPNAME} node is ${NODE} pid is ${NODE_PID}}" kill -9 ${NODE_PID};; n|N) echo "${WARNING}: ${APPNAME} node ${NODE} pid ${NODE_PID} is already start!" continue;; esac else cd ${WORK_DIR}/tomcat_${APPNAME}_${NODE}/bin sh startup.sh > /dev/null 2>&1 timout_start tail -f ../logs/catalina.${DAY}.log| while read line do TAIL_PID=$(ps aux | grep "tail -f ../logs/catalina.${DAY}.log"|awk '{print $2}') if [[ $line =~ .*Server\ startup\ in.* ]];then kill -9 ${TAIL_PID} fi done check_err " ${ERROR} ${APPNAME} node ${NODE} start timeout!" fi fi } stop(){ if [[ "${NODE}" == all ]];then NODE_PID=$(ps aux | grep java | grep tomcat_${APPNAME}| grep -v grep | awk '{print $2}') m=0 for i in ${NODE_PID[@]} do m=$(expr $m+1) log " stop ${APPNAME} node $m pid is $i " kill -9 ${NODE_PID} done else NODE_PID=$(ps aux | grep java | grep tomcat_${APPNAME}_${NODE}|grep -v grep | awk '{print $2}') log " stop ${APPNAME} node ${NODE} pid is ${NODE_PID} " kill -9 ${NODE_PID} fi } status(){ echo "System time is : ${DAY} ${TIME}" echo "${APPNAME} proccess status, node is ${NODE}" echo " ------------------------------------------------- " printf "|\t%-10s|\t%-10s|\t%-10s|\n" namepidstatus echo " ------------------------------------------------- " if [[ "${NODE}" == all ]];then NODES=$(ls ${HOME}/${APPNAME}|grep tomcat_${APPNAME}| wc -l) for i in $(seq 1 ${NODES}) do cd ${HOME}/${APPNAME} NODE_NAME=$(ls tomcat_${APPNAME}_${i}) NODE_PID=$(ps aux | grep java | grep ${NODE_NAME}|grep -v grep | awk '{print $2}') if [[ $(echo ${#NODE_PID}) == 0 ]];then NODE_STATUS=${NULL} else NODE_STATUS=${ALIVE} fi printf "|\t%-10s|\t%-10s|\t%-10s|\n" ${NODE_NAME} ${NODE_PID} ${NODE_STATUS} done else NODE_NAME=$(ls ${HOME}/${APPNAME}/tomcat_${APPNAME}_${NODE}) NODE_PID=$(ps aux | grep java | grep ${NODE_NAME}|grep -v grep | awk '{print $2}') if [[ $(echo ${#NODE_PID}) == 0 ]];then NODE_STATUS='N/A' else NODE_STATUS='Alive' fi printf "|\t%-10s|\t%-10s|\t%-10s|\n"${NODE_NAME} ${NODE_PID} ${NODE_STATUS} fi echo " ------------------------------------------------- " } #變量引用 init_args #執(zhí)行動(dòng)作 case $1 in start) start;; stop) stop;; restart) stop; start;; status) status;; *) template;; esac
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。