linux如何部署微服务启动和停止脚本?

本文在介绍关于部署linux微服务的基础上,重点探讨了微服务启动和停止脚本具体步骤,步骤简单易上手操作,文章内容步步紧凑,希望大家根据这篇文章可以有所收获。

成都创新互联公司主营双河网站建设的网络公司,主营网站建设方案,APP应用开发,双河h5微信平台小程序开发搭建,双河网站营销推广欢迎双河等地区企业咨询

#!/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://myzitong.com/article/pdihhj.html