本文在介紹關(guān)于部署linux微服務(wù)的基礎(chǔ)上,重點(diǎn)探討了微服務(wù)啟動(dòng)和停止腳本具體步驟,步驟簡(jiǎn)單易上手操作,文章內(nèi)容步步緊湊,希望大家根據(jù)這篇文章可以有所收獲。
公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出平湖免費(fèi)做網(wǎng)站回饋大家。#!/bin/bash # # SCRIPT: app_operation.sh # DATE: 2020/03/13 # DESC: 設(shè)置SDWAN應(yīng)用啟停 # AUTHOR: Mright # VERSION: V1.0 # V1.0: 創(chuàng)建后臺(tái)應(yīng)用啟停腳本 # *****************************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}') # 網(wǎng)關(guān)應(yīng)用名 ZUULAPPNAME="example-sdwan-api-gateway-${CURRENTID}.jar" # SITE應(yīng)用名 SITEAPPNAME="example-sdwan-nw-site-${CURRENTID}.jar" # DEVICE應(yīng)用名 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" } # *****************************DEFAULTS USEAGE******************* function checkInput(){ [ ! -d ${JARDIR} ]&& mkdir -p ${JARDIR} #[ ! -d ${SCRIPTDIR} ]&& mkdir -p ${SCRIPTDIR} if [ ${SERNAME} == "NULL" ];then echo "$(log_error)所操作服務(wù)名不能為空,請(qǐng)重新輸入!" defaults exit 10 elif [ ${SERNAME} != "site" -a ${SERNAME} != "eureka" -a ${SERNAME} != "zuul" -a ${SERNAME} != "device" ];then echo "$(log_error)所輸入服務(wù)名錯(cuò)誤,請(qǐng)重新輸入!" 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 "該應(yīng)用暫未記錄" ;; *) defaults ;; esac } function operation(){ case ${SERNAME} in "zuul") case ${OPER} in "stat") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應(yīng)用已啟動(dòng),進(jìn)程ID為$(cat ${ZUULAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應(yīng)用未啟動(dòng)!" fi ;; "start") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應(yīng)用已啟動(dòng),進(jìn)程ID為$(cat ${ZUULAPPPATH}/${SERNAME}.pid)" else cd ${ZUULAPPPATH} echo "$(log_info)${SERNAME} 應(yīng)用準(zhǔn)備啟動(dòng)... " nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${ZUULAPPPATH}/${ZUULAPPNAME} &>/dev/null & # 獲取應(yīng)用進(jìn)程ID寫入pid文件 sleep 10 getPid ${SERNAME} [ ! -f ${ZUULAPPPATH}/${SERNAME}.pid ]&& echo "$(log_error)${SERNAME}啟動(dòng)失敗!"&&exit 12|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." fi ;; "restart") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準(zhǔn)備停止應(yīng)用..." 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}準(zhǔn)備啟動(dòng)應(yīng)用..." #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}啟動(dòng)失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." else echo "$(log_info)${SERNAME}準(zhǔn)備啟動(dòng)應(yīng)用..." 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}啟動(dòng)失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." fi ;; "stop") if [ -f ${ZUULAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準(zhǔn)備停止應(yīng)用..." 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}已經(jīng)停止..." fi ;; esac ;; "site") case ${OPER} in "stat") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應(yīng)用已啟動(dòng),進(jìn)程ID為$(cat ${SITEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應(yīng)用未啟動(dòng)!" fi ;; "start") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應(yīng)用已啟動(dòng),進(jìn)程ID為$(cat ${SITEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應(yīng)用準(zhǔn)備啟動(dòng)... " cd ${SITEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${SITEAPPPATH}/${SITEAPPNAME} &>/dev/null & sleep 10 # 獲取應(yīng)用進(jìn)程ID寫入pid文件 getPid ${SERNAME} [ ! -f ${SITEAPPPATH}/${SERNAME}.pid ]&& echo "$(log_error)${SERNAME}啟動(dòng)失敗!"&&exit 12|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." fi ;; "restart") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準(zhǔn)備停止應(yīng)用..." 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}準(zhǔn)備啟動(dòng)應(yīng)用..." 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}啟動(dòng)失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." else echo "$(log_info)${SERNAME}準(zhǔn)備啟動(dòng)應(yīng)用..." 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}啟動(dòng)失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." fi ;; "stop") if [ -f ${SITEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準(zhǔn)備停止應(yīng)用..." 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}已經(jīng)停止..." fi ;; esac ;; "device") case ${OPER} in "stat") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應(yīng)用已啟動(dòng),進(jìn)程ID為$(cat ${DEVICEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應(yīng)用未啟動(dòng)!" fi ;; "start") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME} 應(yīng)用已啟動(dòng),進(jìn)程ID為$(cat ${DEVICEAPPPATH}/${SERNAME}.pid)" else echo "$(log_info)${SERNAME} 應(yīng)用準(zhǔn)備啟動(dòng)... " cd ${DEVICEAPPPATH} nohup java -Xms1024m -Xmx2048m -jar -Dspring.config.location=./application-1.properties ${DEVICEAPPPATH}/${DEVICEAPPNAME} &>/dev/null & sleep 10 # 獲取應(yīng)用進(jìn)程ID寫入pid文件 getPid ${SERNAME} [ ! -f ${DEVICEAPPPATH}/${SERNAME}.pid ]&& echo "$(log_error)${SERNAME}啟動(dòng)失敗!"&&exit 12|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." fi ;; "restart") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準(zhǔn)備停止應(yīng)用..." 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}準(zhǔn)備啟動(dòng)應(yīng)用..." 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}啟動(dòng)失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." else echo "$(log_info)${SERNAME}準(zhǔn)備啟動(dòng)應(yīng)用..." 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}啟動(dòng)失敗!"&&exit 12;}|| echo "$(log_info)${SERNAME}啟動(dòng)成功,進(jìn)程ID為${SERPID}." fi ;; "stop") if [ -f ${DEVICEAPPPATH}/${SERNAME}.pid ];then echo "$(log_info)${SERNAME}準(zhǔn)備停止應(yīng)用..." 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}已經(jīng)停止..." fi ;; esac ;; "eureka") echo "" ;; *) echo "$(log_error)輸入錯(cuò)誤,請(qǐng)重新輸入!" 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)輸入錯(cuò)誤,請(qǐng)重新輸入!" defaults exit 12 esac } main|tee -a ${LOGNAME}
以上就是linux部署微服務(wù)啟動(dòng)和停止腳本的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊!