您好,登錄后才能下訂單哦!
由于需要對多個項目的多個應用進行管理,項目路徑已經做了標準化部署,所以需要一個統一的腳本來管理所有的啟停操作。
里面附帶日志、進程啟動檢查、批量狀態查看等功能。
以下為腳本相關內容,有些寫法有點low,暫時沒有想到更好的寫法,有興趣的可以留言
#!/bin/bash #用途:本腳本用來管理tomcat節點的啟動、停止、重啟,以及查看tomcat節點的狀態 #作者:張曉龍 #版本v1.0 #定義幫助說明函數,語法錯誤時返回使用說明 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獲取應用名稱、節點數,節點數可以為所有 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 #語法錯誤 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#語法錯誤 template fi if [[ "$3" =~ -. ]];then NODE="$2"; #判斷輸入的節點,如果不是數字或者all,則進行報錯并退出 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啟動定時器,低于60秒繼續執行腳本,高于記錄報錯信息 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 #檢查是否有進程存活,如果有,做出判斷,是殺掉后再啟動,還是保留,只啟動未啟動的 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 #如果指定了節點,那么就判斷節點是否是啟動狀態,如果啟動,殺掉后再啟動 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 #執行動作 case $1 in start) start;; stop) stop;; restart) stop; start;; status) status;; *) template;; esac
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。