重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

linux如何部署微服务启动和停止脚本?-创新互联

本文在介绍关于部署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  "
}

# *****************************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部署微服务启动和停止脚本的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注创新互联网站制作公司行业资讯!


文章标题:linux如何部署微服务启动和停止脚本?-创新互联
文章位置:http://cqcxhl.cn/article/diihhc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP