您好,登錄后才能下訂單哦!
本文在介紹關于部署linux微服務的基礎上,重點探討了微服務啟動和停止腳本具體步驟,步驟簡單易上手操作,文章內容步步緊湊,希望大家根據這篇文章可以有所收獲。
#!/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}
以上就是linux部署微服務啟動和停止腳本的詳細內容了,看完之后是否有所收獲呢?如果想了解更多相關內容,歡迎關注億速云行業資訊!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。