話很少說直接上腳本,比較簡單基本都能看懂,無非是啓動應用先後加了一些判斷而已。java
#!/bin/bash # # SCRIPT: app_operation.sh # DATE: 2020/03/13 # DESC: 設置SDWAN應用啓停 # AUTHOR: Mright # VERSION: V1.0 # V1.0: 建立後臺應用啓停腳本 # *****************************BEGIN ARGUS************************* set -o pipefail source ~/.bash_profile SERNAME=$1 INPUT=$2 SERNAME=${SERNAME:="NULL"} OPER=${INPUT:="stat"} SERPID="null" JARDIR="${HOME}" SCRIPTDIR="${JARDIR}" TMPPID="${JARDIR}" LOGDIR="/tmp" LOGNAME="/tmp/app_operation_$(hostname).log" APPLOGNAME="${JARDIR}/logs/${INPUT}.log" APPINFOFILE="${HOME}/version.info" CURRENTID=$(cat ${APPINFOFILE}|grep "${SERNAME}_latest"|awk -F ':' '{print $2}') # 網關應用名 ZUULAPPNAME="example-sdwan-api-gateway-${CURRENTID}.jar" # SITE應用名 SITEAPPNAME="example-sdwan-nw-site-${CURRENTID}.jar" # DEVICE應用名 DEVICEAPPNAME="example-sdwan-nw-device-${CURRENTID}.jar" BASEPATH="${HOME}" ZUULAPPPATH="${BASEPATH}/example-sdwan-api-gateway" SITEAPPPATH="${BASEPATH}/example-sdwan-nw-site" DEVICEAPPPATH="${BASEPATH}/example-sdwan-nw-device" # *****************************END ARGUS************************* # *****************************DEFAULTS USEAGE******************* function defaults(){ echo "eg. sh app_operation.sh <site|zuul|device> <stat|start|stop|restart>" } # *****************************DEFAULTS USEAGE******************* function checkInput(){ [ ! -d ${JARDIR} ]&& mkdir -p ${JARDIR} #[ ! -d ${SCRIPTDIR} ]&& mkdir -p ${SCRIPTDIR} if [ ${SERNAME} == "NULL" ];then echo "$(log_error)所操做服務名不能爲空,請從新輸入!" defaults exit 10 elif [ ${SERNAME} != "site" -a ${SERNAME} != "eureka" -a ${SERNAME} != "zuul" -a ${SERNAME} != "device" ];then echo "$(log_error)所輸入服務名錯誤,請從新輸入!" defaults exit 11 fi } # ******************** FUNCTION log_info BEGIN ************************** function log_info(){ echo "[$(date +'%Y-%m-%d %H:%M:%S')][INFO]" } # ******************** FUNCTION log_info END *************************** # ******************** FUNCTION log_info BEGIN ************************** function log_error(){ echo "[$(date +'%Y-%m-%d %H:%M:%S')][ERROR]" } # ******************** FUNCTION log_info END *************************** function getPid(){ case ${SERNAME} in "zuul") declare -a FLAG count=0 PATTERN=$(echo "${ZUULAPPNAME}"|grep -o ".*[^0-9.jar]"|sed -r "s/-$//g") SERPID=$(ps -ef|grep "${PATTERN}"|grep -v "grep"|awk '{print $2}') #SERPID=${SERPID:=110} for pro in ${SERPID};do FLAG[$count]=$pro let count++ done if [ ${#FLAG[*]} -gt 0 ];then echo ${SERPID}> ${ZUULAPPPATH}/${SERNAME}.pid else [ -f ${ZUULAPPPATH}/${SERNAME}.pid ]&&{ rm -f ${ZUULAPPPATH}/${SERNAME}.pid;} fi ;; "device") declare -a FLAG count=0 PATTERN=$(echo "${DEVICEAPPNAME}"|grep -o ".*[^0-9.jar]"|sed -r "s/-$//g") SERPID=$(ps -ef|grep "${PATTERN}"|grep -v "grep"|awk '{print $2}') #SERPID=${SERPID:=110} for pro in ${SERPID};do FLAG[$count]=$pro let count++ done if [ ${#FLAG[*]} -gt 0 ];then echo ${SERPID}> ${DEVICEAPPPATH}/${SERNAME}.pid else [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ]&&{ rm -f ${DEVICEAPPPATH}/${SERNAME}.pid;} fi ;; "site") declare -a FLAG count=0 PATTERN=$(echo "${SITEAPPNAME}"|grep -o ".*[^0-9.jar]"|sed -r "s/-$//g") SERPID=$(ps -ef|grep "${PATTERN}"|grep -v "grep"|awk '{print $2}') #SERPID=${SERPID:=110} for pro in ${SERPID};do FLAG[$count]=$pro let count++ done if [ ${#FLAG[*]} -gt 0 ];then echo ${SERPID}> ${SITEAPPPATH}/${SERNAME}.pid else [ -f ${SITEAPPPATH}/${SERNAME}.pid ]&&{ rm -f ${SITEAPPPATH}/${SERNAME}.pid;} fi ;; "eureka") echo "該應用暫未記錄" ;; *) defaults ;; esac } function operation(){ case ${SERNAME} in "zuul") case ${OPER} in "stat") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應用已啓動,進程ID爲$(cat ${ZUULAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應用未啓動!" fi ;; "start") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應用已啓動,進程ID爲$(cat ${ZUULAPPPATH}/${SERNAME}.pid)" else cd ${ZUULAPPPATH} echo "$(log_info)${SERNAME} 應用準備啓動... " nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${ZUULAPPPATH}/${ZUULAPPNAME} &>/dev/null & # 獲取應用進程ID寫入pid文件 sleep 10 getPid ${SERNAME} [ ! -f ${ZUULAPPPATH}/${SERNAME}.pid ]&& echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." fi ;; "restart") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準備中止應用..." kill $(cat ${ZUULAPPPATH}/${SERNAME}.pid) sleep 3 [ $? -eq 0 ]&& rm -f ${ZUULAPPPATH}/${SERNAME}.pid &&echo "$(log_info)${SERNAME}已中止。"||{ echo "$(log_error)${SERNAME}中止失敗。"&&exit 13;} echo "$(log_info)${SERNAME}準備啓動應用..." #nohup java -jar ${ZUULAPPPATH}/${ZUULAPPNAME} &>/dev/null & cd ${ZUULAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${ZUULAPPPATH}/${ZUULAPPNAME} &>/dev/null & sleep 10 getPid ${SERNAME} [ ! -f ${ZUULAPPPATH}/${SERNAME}.pid ]&&{ echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." else echo "$(log_info)${SERNAME}準備啓動應用..." cd ${ZUULAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${ZUULAPPPATH}/${ZUULAPPNAME} &>/dev/null & sleep 10 getPid ${SERNAME} [ ! -f ${ZUULAPPPATH}/${SERNAME}.pid ]&&{ echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." fi ;; "stop") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準備中止應用..." kill $(cat ${ZUULAPPPATH}/${SERNAME}.pid) sleep 3 [ $? -eq 0 ]&& rm -f ${ZUULAPPPATH}/${SERNAME}.pid &&echo "$(log_info)${SERNAME}已中止。"||{ echo "$(log_error)${SERNAME}中止失敗。"&&exit 13;} else echo "$(log_info)${SERNAME}已經中止..." fi ;; esac ;; "site") case ${OPER} in "stat") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應用已啓動,進程ID爲$(cat ${SITEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應用未啓動!" fi ;; "start") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應用已啓動,進程ID爲$(cat ${SITEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應用準備啓動... " cd ${SITEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${SITEAPPPATH}/${SITEAPPNAME} &>/dev/null & sleep 10 # 獲取應用進程ID寫入pid文件 getPid ${SERNAME} [ ! -f ${SITEAPPPATH}/${SERNAME}.pid ]&& echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." fi ;; "restart") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準備中止應用..." kill $(cat ${SITEAPPPATH}/${SERNAME}.pid) sleep 3 [ $? -eq 0 ]&& rm -f ${SITEAPPPATH}/${SERNAME}.pid &&echo "$(log_info)${SERNAME}已中止。"||{ echo "$(log_error)${SERNAME}中止失敗。"&&exit 13;} echo "$(log_info)${SERNAME}準備啓動應用..." cd ${SITEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${SITEAPPPATH}/${SITEAPPNAME} &>/dev/null & sleep 10 getPid ${SERNAME} [ ! -f ${SITEAPPPATH}/${SERNAME}.pid ]&&{ echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." else echo "$(log_info)${SERNAME}準備啓動應用..." cd ${SITEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${SITEAPPPATH}/${SITEAPPNAME} &>/dev/null & sleep 10 getPid ${SERNAME} [ ! -f ${SITEAPPPATH}/${SERNAME}.pid ]&&{ echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." fi ;; "stop") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準備中止應用..." kill $(cat ${SITEAPPPATH}/${SERNAME}.pid) sleep 3 [ $? -eq 0 ]&& rm -f ${SITEAPPPATH}/${SERNAME}.pid &&echo "$(log_info)${SERNAME}已中止。"||{ echo "$(log_error)${SERNAME}中止失敗。"&&exit 13;} else echo "$(log_info)${SERNAME}已經中止..." fi ;; esac ;; "device") case ${OPER} in "stat") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應用已啓動,進程ID爲$(cat ${DEVICEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應用未啓動!" fi ;; "start") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應用已啓動,進程ID爲$(cat ${DEVICEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應用準備啓動... " cd ${DEVICEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${DEVICEAPPPATH}/${DEVICEAPPNAME} &>/dev/null & sleep 10 # 獲取應用進程ID寫入pid文件 getPid ${SERNAME} [ ! -f ${DEVICEAPPPATH}/${SERNAME}.pid ]&& echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." fi ;; "restart") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準備中止應用..." kill $(cat ${DEVICEAPPPATH}/${SERNAME}.pid) sleep 3 [ $? -eq 0 ]&& rm -f ${DEVICEAPPPATH}/${SERNAME}.pid &&echo "$(log_info)${SERNAME}已中止。"||{ echo "$(log_error)${SERNAME}中止失敗。"&&exit 13;} echo "$(log_info)${SERNAME}準備啓動應用..." cd ${DEVICEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${DEVICEAPPPATH}/${DEVICEAPPNAME} &>/dev/null & sleep 10 getPid ${SERNAME} [ ! -f ${DEVICEAPPPATH}/${SERNAME}.pid ]&&{ echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." else echo "$(log_info)${SERNAME}準備啓動應用..." cd ${DEVICEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${DEVICEAPPPATH}/${DEVICEAPPNAME} &>/dev/null & sleep 10 getPid ${SERNAME} [ ! -f ${DEVICEAPPPATH}/${SERNAME}.pid ]&&{ echo "$(log_error)${SERNAME}啓動失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啓動成功,進程ID爲${SERPID}." fi ;; "stop") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準備中止應用..." kill $(cat ${DEVICEAPPPATH}/${SERNAME}.pid) sleep 3 [ $? -eq 0 ]&& rm -f ${DEVICEAPPPATH}/${SERNAME}.pid &&echo "$(log_info)${SERNAME}已中止。"||{ echo "$(log_error)${SERNAME}中止失敗。"&&exit 13;} else echo "$(log_info)${SERNAME}已經中止..." fi ;; esac ;; "eureka") echo "" ;; *) echo "$(log_error)輸入錯誤,請從新輸入!" defaults exit 12 esac } function main(){ case $OPER in "stat") checkInput getPid ${SERNAME} operation ${SERNAME} ;; "start") checkInput getPid ${SERNAME} operation ${SERNAME} ;; "stop") checkInput getPid ${SERNAME} operation ${SERNAME} ;; "restart") checkInput getPid ${SERNAME} operation ${SERNAME} ;; *) echo "$(log_error)輸入錯誤,請從新輸入!" defaults exit 12 esac } main|tee -a ${LOGNAME}