本文在介绍关于部署linux微服务的基础上,重点探讨了微服务启动和停止脚本具体步骤,步骤简单易上手操作,文章内容步步紧凑,希望大家根据这篇文章可以有所收获。
#!/bin/bash##SCRIPT:app_operation.sh#DATE:2020/03/13#DESC:设置SDWAN应用启停#AUTHOR:Mright#VERSION:V1.0#V1.0:创建后台应用启停脚本#*****************************BEGINARGUS*************************set-opipefailsource~/.bash_profileSERNAME=$1INPUT=$2SERNAME=${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"#*****************************ENDARGUS*************************#*****************************DEFAULTSUSEAGE*******************functiondefaults(){echo"eg.shapp_operation.sh<site|zuul|device><stat|start|stop|restart>"}#*****************************DEFAULTSUSEAGE*******************functioncheckInput(){[!-d${JARDIR}]&&mkdir-p${JARDIR}#[!-d${SCRIPTDIR}]&&mkdir-p${SCRIPTDIR}if[${SERNAME}=="NULL"];thenecho"$(log_error)所操作服务名不能为空,请重新输入!"defaultsexit10elif[${SERNAME}!="site"-a${SERNAME}!="eureka"-a${SERNAME}!="zuul"-a${SERNAME}!="device"];thenecho"$(log_error)所输入服务名错误,请重新输入!"defaultsexit11fi}#********************FUNCTIONlog_infoBEGIN**************************functionlog_info(){echo"[$(date+'%Y-%m-%d%H:%M:%S')][INFO]"}#********************FUNCTIONlog_infoEND***************************#********************FUNCTIONlog_infoBEGIN**************************functionlog_error(){echo"[$(date+'%Y-%m-%d%H:%M:%S')][ERROR]"}#********************FUNCTIONlog_infoEND***************************functiongetPid(){case${SERNAME}in"zuul")declare-aFLAGcount=0PATTERN=$(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}forproin${SERPID};doFLAG[$count]=$proletcount++doneif[${#FLAG}-gt0];thenecho${SERPID}>${ZUULAPPPATH}/${SERNAME}.pidelse[-f${ZUULAPPPATH}/${SERNAME}.pid]&&{rm-f${ZUULAPPPATH}/${SERNAME}.pid;}fi;;"device")declare-aFLAGcount=0PATTERN=$(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}forproin${SERPID};doFLAG[$count]=$proletcount++doneif[${#FLAG}-gt0];thenecho${SERPID}>${DEVICEAPPPATH}/${SERNAME}.pidelse[-f${DEVICEAPPPATH}/${SERNAME}.pid]&&{rm-f${DEVICEAPPPATH}/${SERNAME}.pid;}fi;;"site")declare-aFLAGcount=0PATTERN=$(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}forproin${SERPID};doFLAG[$count]=$proletcount++doneif[${#FLAG}-gt0];thenecho${SERPID}>${SITEAPPPATH}/${SERNAME}.pidelse[-f${SITEAPPPATH}/${SERNAME}.pid]&&{rm-f${SITEAPPPATH}/${SERNAME}.pid;}fi;;"eureka")echo"该应用暂未记录";;*)defaults;;esac}functionoperation(){case${SERNAME}in"zuul")case${OPER}in"stat")if[-f${ZUULAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}应用已启动,进程ID为$(cat${ZUULAPPPATH}/${SERNAME}.pid)"elseecho"$(log_info)${SERNAME}应用未启动!"fi;;"start")if[-f${ZUULAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}应用已启动,进程ID为$(cat${ZUULAPPPATH}/${SERNAME}.pid)"elsecd${ZUULAPPPATH}echo"$(log_info)${SERNAME}应用准备启动..."nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${ZUULAPPPATH}/${ZUULAPPNAME}&>/dev/null&#获取应用进程ID写入pid文件sleep10getPid${SERNAME}[!-f${ZUULAPPPATH}/${SERNAME}.pid]&&echo"$(log_error)${SERNAME}启动失败!"&&exit12||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."fi;;"restart")if[-f${ZUULAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}准备停止应用..."kill$(cat${ZUULAPPPATH}/${SERNAME}.pid)sleep3[$?-eq0]&&rm-f${ZUULAPPPATH}/${SERNAME}.pid&&echo"$(log_info)${SERNAME}已停止。"||{echo"$(log_error)${SERNAME}停止失败。"&&exit13;}echo"$(log_info)${SERNAME}准备启动应用..."#nohupjava-jar${ZUULAPPPATH}/${ZUULAPPNAME}&>/dev/null&cd${ZUULAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${ZUULAPPPATH}/${ZUULAPPNAME}&>/dev/null&sleep10getPid${SERNAME}[!-f${ZUULAPPPATH}/${SERNAME}.pid]&&{echo"$(log_error)${SERNAME}启动失败!"&&exit12;}||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."elseecho"$(log_info)${SERNAME}准备启动应用..."cd${ZUULAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${ZUULAPPPATH}/${ZUULAPPNAME}&>/dev/null&sleep10getPid${SERNAME}[!-f${ZUULAPPPATH}/${SERNAME}.pid]&&{echo"$(log_error)${SERNAME}启动失败!"&&exit12;}||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."fi;;"stop")if[-f${ZUULAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}准备停止应用..."kill$(cat${ZUULAPPPATH}/${SERNAME}.pid)sleep3[$?-eq0]&&rm-f${ZUULAPPPATH}/${SERNAME}.pid&&echo"$(log_info)${SERNAME}已停止。"||{echo"$(log_error)${SERNAME}停止失败。"&&exit13;}elseecho"$(log_info)${SERNAME}已经停止..."fi;;esac;;"site")case${OPER}in"stat")if[-f${SITEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}应用已启动,进程ID为$(cat${SITEAPPPATH}/${SERNAME}.pid)"elseecho"$(log_info)${SERNAME}应用未启动!"fi;;"start")if[-f${SITEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}应用已启动,进程ID为$(cat${SITEAPPPATH}/${SERNAME}.pid)"elseecho"$(log_info)${SERNAME}应用准备启动..."cd${SITEAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${SITEAPPPATH}/${SITEAPPNAME}&>/dev/null&sleep10#获取应用进程ID写入pid文件getPid${SERNAME}[!-f${SITEAPPPATH}/${SERNAME}.pid]&&echo"$(log_error)${SERNAME}启动失败!"&&exit12||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."fi;;"restart")if[-f${SITEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}准备停止应用..."kill$(cat${SITEAPPPATH}/${SERNAME}.pid)sleep3[$?-eq0]&&rm-f${SITEAPPPATH}/${SERNAME}.pid&&echo"$(log_info)${SERNAME}已停止。"||{echo"$(log_error)${SERNAME}停止失败。"&&exit13;}echo"$(log_info)${SERNAME}准备启动应用..."cd${SITEAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${SITEAPPPATH}/${SITEAPPNAME}&>/dev/null&sleep10getPid${SERNAME}[!-f${SITEAPPPATH}/${SERNAME}.pid]&&{echo"$(log_error)${SERNAME}启动失败!"&&exit12;}||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."elseecho"$(log_info)${SERNAME}准备启动应用..."cd${SITEAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${SITEAPPPATH}/${SITEAPPNAME}&>/dev/null&sleep10getPid${SERNAME}[!-f${SITEAPPPATH}/${SERNAME}.pid]&&{echo"$(log_error)${SERNAME}启动失败!"&&exit12;}||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."fi;;"stop")if[-f${SITEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}准备停止应用..."kill$(cat${SITEAPPPATH}/${SERNAME}.pid)sleep3[$?-eq0]&&rm-f${SITEAPPPATH}/${SERNAME}.pid&&echo"$(log_info)${SERNAME}已停止。"||{echo"$(log_error)${SERNAME}停止失败。"&&exit13;}elseecho"$(log_info)${SERNAME}已经停止..."fi;;esac;;"device")case${OPER}in"stat")if[-f${DEVICEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}应用已启动,进程ID为$(cat${DEVICEAPPPATH}/${SERNAME}.pid)"elseecho"$(log_info)${SERNAME}应用未启动!"fi;;"start")if[-f${DEVICEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}应用已启动,进程ID为$(cat${DEVICEAPPPATH}/${SERNAME}.pid)"elseecho"$(log_info)${SERNAME}应用准备启动..."cd${DEVICEAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${DEVICEAPPPATH}/${DEVICEAPPNAME}&>/dev/null&sleep10#获取应用进程ID写入pid文件getPid${SERNAME}[!-f${DEVICEAPPPATH}/${SERNAME}.pid]&&echo"$(log_error)${SERNAME}启动失败!"&&exit12||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."fi;;"restart")if[-f${DEVICEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}准备停止应用..."kill$(cat${DEVICEAPPPATH}/${SERNAME}.pid)sleep3[$?-eq0]&&rm-f${DEVICEAPPPATH}/${SERNAME}.pid&&echo"$(log_info)${SERNAME}已停止。"||{echo"$(log_error)${SERNAME}停止失败。"&&exit13;}echo"$(log_info)${SERNAME}准备启动应用..."cd${DEVICEAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${DEVICEAPPPATH}/${DEVICEAPPNAME}&>/dev/null&sleep10getPid${SERNAME}[!-f${DEVICEAPPPATH}/${SERNAME}.pid]&&{echo"$(log_error)${SERNAME}启动失败!"&&exit12;}||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."elseecho"$(log_info)${SERNAME}准备启动应用..."cd${DEVICEAPPPATH}nohupjava-Xms1024m-Xmx2048m-jar-Dspring.config.location=./application-1.properties${DEVICEAPPPATH}/${DEVICEAPPNAME}&>/dev/null&sleep10getPid${SERNAME}[!-f${DEVICEAPPPATH}/${SERNAME}.pid]&&{echo"$(log_error)${SERNAME}启动失败!"&&exit12;}||echo"$(log_info)${SERNAME}启动成功,进程ID为${SERPID}."fi;;"stop")if[-f${DEVICEAPPPATH}/${SERNAME}.pid];thenecho"$(log_info)${SERNAME}准备停止应用..."kill$(cat${DEVICEAPPPATH}/${SERNAME}.pid)sleep3[$?-eq0]&&rm-f${DEVICEAPPPATH}/${SERNAME}.pid&&echo"$(log_info)${SERNAME}已停止。"||{echo"$(log_error)${SERNAME}停止失败。"&&exit13;}elseecho"$(log_info)${SERNAME}已经停止..."fi;;esac;;"eureka")echo"";;*)echo"$(log_error)输入错误,请重新输入!"defaultsexit12esac}functionmain(){case$OPERin"stat")checkInputgetPid${SERNAME}operation${SERNAME};;"start")checkInputgetPid${SERNAME}operation${SERNAME};;"stop")checkInputgetPid${SERNAME}operation${SERNAME};;"restart")checkInputgetPid${SERNAME}operation${SERNAME};;*)echo"$(log_error)输入错误,请重新输入!"defaultsexit12esac}main|tee-a${LOGNAME}以上就是linux部署微服务启动和停止脚本的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注Vecloud行业资讯!
|