升级微服务了,一个一个起包太麻烦,自己写了个一键启动
之前只有一个包,简单写个脚本就好了,现在一次要5个包,就花了点时间写了个启动脚本。可以做到以下功能:
一键启动、停止、重启、查看状态
可以对所有包进行操作
也可以对单独指定的包进行操作
起包的时候是一个一个起的,只有当排在前面的包启动成功,才会起下一个包,并且会输出pid,端口以及启动耗时,方便查看异常。
脚本上可以指定端口,可以指定配置文件,并可全局调整内存,当然你也可以对个别包进行设置。
自己也不是搞运维的,网上参考了很多,也查了很多文档,但还是有很多不足,比如起包的时候,虽然设置了变量,以便启动脚本可以复制黏贴,但确实不简洁,应该可以用数组+循环实现,还在研究。
如果大佬有更好的实现方式,也请指正
如下
(如果linux启动不了,大概率是格式的问题,用dos2unix转换一下即可)#!/bin/sh
# 修改项目名称、端口列表、复制启动、停止脚本、修改all相关、修改help相关
# 项目名称
export proj_name=scrm
export proj_version=2.4.0
# 项目部署目录
export projectDir=/www/java/jar/$proj_name/
# 项目日志目录
export logDir=/www/java/log/$proj_name/
# 包的列表
export GATE=$proj_name-cloud-gateway-$proj_version.jar
export SYST=$proj_name-cloud-system-start-$proj_version.jar
export DEMO=$proj_name-cloud-demo-$proj_version.jar
export MINI=$proj_name-cloud-miniprogram-$proj_version.jar
export OFFI=$proj_name-cloud-offiaccount-$proj_version.jar
# 端口列表
export GATE_port=9997
export SYST_port=7010
export DEMO_port=7011
export MINI_port=7012
export OFFI_port=7013
# 启动参数
export Xms=-Xms256m
export Xmx=-Xmx512m
export profileActive=prod
# 这里的-x 参数判断${logDir}是否存在并且是否具有可执行权限
if [ ! -x "${logDir}" ]; then
mkdir -p "${logDir}"
fi
case "$1" in
help)
echo "这是$proj_name启动脚本,应优先启动网关GATE"
echo "JAR包目录为$projectDir"
echo -e "\033[32m可用的命令为:\033[0m"
echo -e "\033[32mstart|stop|restart|status \033[0m"
echo "--启动:start:启动,如应用已启动,则仅输出应用状态,不重新启动"
echo "--停止:stop :停止"
echo "--状态:status :查看应用状态"
echo "--重启:restart:重新启动"
echo "可启动的程序为:"
echo "--网关:GATE:$GATE"
echo "--系统:SYST:$SYST"
echo "--应用:DEMO:$DEMO"
echo "--小程序:MINI:$MINI"
echo "--公众号:OFFI:$OFFI"
echo "可输入all,对所有应用进行操作,启动|重启时不要退出shell命令,直到系统提示启动成功"
echo "示例:"
echo -e "启动网关:\033[1m$0 start gate \033[0m"
echo -e "停止全部:\033[1m$0 stop all \033[0m"
echo -e "后台启动:\033[1m$0 start gate &\033[0m,此命令不应关闭宝塔终端或其他终端,否则会停止"
echo -e "后台启动:\033[1mnohup $0 start gate &\033[0m,此命令可以关闭宝塔终端或其他终端"
echo "程序的缩写大小写均可哈"
;;
start)
case "$2" in
gate | GATE)
JAR=$GATE
JAR_port=$GATE_port
count=$(ps -ef | grep ${JAR} | grep -v "grep" | wc -l)
if [ ${count} -lt 1 ]; then
cd ${projectDir}
start=$(date +%s)
nohup java $Xms $Xmx -Dfile.encoding=utf-8 -jar ${JAR} --spring.profiles.active=${profileActive} --server.port=${JAR_port} >${logDir}${JAR}_$(date +%Y-%m-%d_%H:%M:%S).log 2>&1 &
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 ${JAR} 程序 ... ..."
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
until [ -n "$jarPid" ]; do
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
done
end=$(date +%s)
time=$(echo $start $end | awk '{print $2-$1}')
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 启动成功,进程id: ${jarPid},耗时$time秒\033[0m"
else
jarPid=$(ps x | grep ${JAR} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
echo -e "\033[31m已经存在 ${count} 个 ${JAR} 程序在运行,进程id: ${jarPid}\033[0m"
fi
;;
demo | DEMO)
JAR=$DEMO
JAR_port=$DEMO_port
count=$(ps -ef | grep ${JAR} | grep -v "grep" | wc -l)
if [ ${count} -lt 1 ]; then
cd ${projectDir}
start=$(date +%s)
nohup java $Xms $Xmx -Dfile.encoding=utf-8 -jar ${JAR} --spring.profiles.active=${profileActive} --server.port=${JAR_port} >${logDir}${JAR}_$(date +%Y-%m-%d_%H:%M:%S).log 2>&1 &
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 ${JAR} 程序 ... ..."
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
until [ -n "$jarPid" ]; do
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
done
end=$(date +%s)
time=$(echo $start $end | awk '{print $2-$1}')
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 启动成功,进程id: ${jarPid},耗时$time秒\033[0m"
else
jarPid=$(ps x | grep ${JAR} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
echo -e "\033[31m已经存在 ${count} 个 ${JAR} 程序在运行,进程id: ${jarPid}\033[0m"
fi
;;
mini | MINI)
JAR=$MINI
JAR_port=$MINI_port
count=$(ps -ef | grep ${JAR} | grep -v "grep" | wc -l)
if [ ${count} -lt 1 ]; then
cd ${projectDir}
start=$(date +%s)
nohup java $Xms $Xmx -Dfile.encoding=utf-8 -jar ${JAR} --spring.profiles.active=${profileActive} --server.port=${JAR_port} >${logDir}${JAR}_$(date +%Y-%m-%d_%H:%M:%S).log 2>&1 &
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 ${JAR} 程序 ... ..."
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
until [ -n "$jarPid" ]; do
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
done
end=$(date +%s)
time=$(echo $start $end | awk '{print $2-$1}')
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 启动成功,进程id: ${jarPid},耗时$time秒\033[0m"
else
jarPid=$(ps x | grep ${JAR} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
echo -e "\033[31m已经存在 ${count} 个 ${JAR} 程序在运行,进程id: ${jarPid}\033[0m"
fi
;;
offi | OFFI)
JAR=$OFFI
JAR_port=$OFFI_port
count=$(ps -ef | grep ${JAR} | grep -v "grep" | wc -l)
if [ ${count} -lt 1 ]; then
cd ${projectDir}
start=$(date +%s)
nohup java $Xms $Xmx -Dfile.encoding=utf-8 -jar ${JAR} --spring.profiles.active=${profileActive} --server.port=${JAR_port} >${logDir}${JAR}_$(date +%Y-%m-%d_%H:%M:%S).log 2>&1 &
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 ${JAR} 程序 ... ..."
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
until [ -n "$jarPid" ]; do
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
done
end=$(date +%s)
time=$(echo $start $end | awk '{print $2-$1}')
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 启动成功,进程id: ${jarPid},耗时$time秒\033[0m"
else
jarPid=$(ps x | grep ${JAR} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
echo -e "\033[31m已经存在 ${count} 个 ${JAR} 程序在运行,进程id: ${jarPid}\033[0m"
fi
;;
syst | SYST)
JAR=$SYST
JAR_port=$SYST_port
count=$(ps -ef | grep ${JAR} | grep -v "grep" | wc -l)
if [ ${count} -lt 1 ]; then
cd ${projectDir}
start=$(date +%s)
nohup java $Xms $Xmx -Dfile.encoding=utf-8 -jar ${JAR} --spring.profiles.active=${profileActive} --server.port=${JAR_port} >${logDir}${JAR}_$(date +%Y-%m-%d_%H:%M:%S).log 2>&1 &
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 ${JAR} 程序 ... ..."
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
until [ -n "$jarPid" ]; do
jarPid=$(lsof -i:${JAR_port} | grep "LISTEN" | awk '{print $2}')
done
end=$(date +%s)
time=$(echo $start $end | awk '{print $2-$1}')
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 启动成功,进程id: ${jarPid},耗时$time秒\033[0m"
else
jarPid=$(ps x | grep ${JAR} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
echo -e "\033[31m已经存在 ${count} 个 ${JAR} 程序在运行,进程id: ${jarPid}\033[0m"
fi
;;
all | ALL)
start1=$(date +%s)
$0 start GATE
$0 start SYST
$0 start DEMO
$0 start MINI
$0 start OFFI
end1=$(date +%s)
time1=$(echo $start1 $end1 | awk '{print $2-$1}')
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') 所有程序均已启动,耗时共计$time1秒\033[0m"
;;
esac
;;
stop)
case "$2" in
gate | GATE)
JAR=$GATE
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 并未运行"
else
kill -9 $P_ID
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 已成功停止\033[0m"
fi
;;
demo | DEMO)
JAR=$DEMO
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 并未运行"
else
kill -9 $P_ID
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 已成功停止\033[0m"
fi
;;
mini | MINI)
JAR=$MINI
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 并未运行"
else
kill -9 $P_ID
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 已成功停止\033[0m"
fi
;;
offi | OFFI)
JAR=$OFFI
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 并未运行"
else
kill -9 $P_ID
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 已成功停止\033[0m"
fi
;;
syst | SYST)
JAR=$SYST
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 并未运行"
else
kill -9 $P_ID
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') ${JAR} 已成功停止\033[0m"
fi
;;
all | ALL)
$0 stop GATE
$0 stop DEMO
$0 stop MINI
$0 stop OFFI
$0 stop SYST
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') 所有程序均已关闭\033[0m"
;;
esac
;;
status)
case "$2" in
gate | GATE)
JAR=$GATE
JAR_port=$GATE_port
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo -e "\033[31m${JAR} 并未运行\033[0m"
else
echo -e "\033[32m${JAR} 正在运行,PID为${P_ID},端口号为${JAR_port}\033[0m"
fi
;;
domo | DEMO)
JAR=$DEMO
JAR_port=$DEMO_port
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo -e "\033[31m${JAR} 并未运行\033[0m"
else
echo -e "\033[32m${JAR} 正在运行,PID为${P_ID},端口号为${JAR_port}\033[0m"
fi
;;
mini | MINI)
JAR=$MINI
JAR_port=$MINI_port
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo -e "\033[31m${JAR} 并未运行\033[0m"
else
echo -e "\033[32m${JAR} 正在运行,PID为${P_ID},端口号为${JAR_port}\033[0m"
fi
;;
offi | OFFI)
JAR=$OFFI
JAR_port=$OFFI_port
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo -e "\033[31m${JAR} 并未运行\033[0m"
else
echo -e "\033[32m${JAR} 正在运行,PID为${P_ID},端口号为${JAR_port}\033[0m"
fi
;;
syst | SYST)
JAR=$SYST
JAR_port=$SYST_port
P_ID=$(ps -ef | grep -w ${JAR} | grep -v "grep" | awk '{print $2}')
if [ "$P_ID" == "" ]; then
echo -e "\033[31m${JAR} 并未运行\033[0m"
else
echo -e "\033[32m${JAR} 正在运行,PID为${P_ID},端口号为${JAR_port}\033[0m"
fi
;;
all | ALL)
$0 status GATE
$0 status DEMO
$0 status MINI
$0 status OFFI
$0 status SYST
;;
esac
;;
restart)
$0 stop $2
sleep 2
$0 start $2
echo -e "\033[32m$(date '+%Y-%m-%d %H:%M:%S') 所有程序均已重启成功\033[0m"
;;
esac
本帖最后由 shiningwhh 于 2021-1-26 13:02 编辑
贴几张运行图片:
帮助命令:
查看应用状态:
重启应用:
用docker-compose不就行了吗 自己也不是搞运维的,网上参考了很多,上海快3也查了很多文档,但还是有很多不足,体彩排列5比如起包的时候,虽然设置了变量,以便启动脚本可以复制黏贴
页:
[1]