WechatGroup

This commit is contained in:
Lishundong
2025-09-22 17:17:48 +08:00
parent 410182ec79
commit 510e179c1d

View File

@@ -1,61 +1,149 @@
#!/usr/bin/env bash
# 配置部分
BASE_PATH=/root/backend
DES_PATH=/root/backend
JAR_PATH=${DES_PATH}/ruoyi-admin/target/ruoyi-admin.jar
LOG_PATH=${DES_PATH}/logs
LOG_FILE=${LOG_PATH}/backend.log
BACK_LOG=${LOG_PATH}/back/backend-info.log
MODEL_NAME=backend
PROFILE=dev
# JVM配置
JVM_MEMORY=" -Xms256M -Xmx256M -XX:MaxDirectMemorySize=256M"
JVM_DEBUG=" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6011"
JVM_OPTION="${JVM_MEMORY} ${JVM_DEBUG}"
_math() {
_m_opts="$@"
printf "%s" "$(($_m_opts))"
printf "%s" "$(($@))"
}
# 输出绿色文字
__green() {
printf '\33[1;32m%b\33[0m' "$1" "\n"
return
}
# 输出红色文字
__red() {
printf '\33[1;31m%b\33[0m' "$1" "\n"
return
}
__kill() {
PID=`ps -ef | grep ${1}.jar | grep -v grep |awk '{print $2}' | xargs`
sleepSeconds=10
curSleepSecond=1
while [[ -n "$PID" && "$sleepSeconds" -ge "$curSleepSecond" ]]; do
__green "尝试kill PID : ${PID}"
if [ "$sleepSeconds" -eq "$curSleepSecond" ]; then
__red "强制关闭: ${curSleepSecond}"
kill -9 $PID >> /dev/null 2>&1
fi
kill $PID >> /dev/null 2>&1
__green "停止程序计时:${curSleepSecond}"
sleep 1
curSleepSecond="$(_math "$curSleepSecond" + 1)"
PID=`ps -ef | grep ${1}.jar | grep -v grep |awk '{print $2}' | xargs`
done
}
source /etc/profile
git --git-dir=/root/backend/.git --work-tree=/root/backend fetch origin main
git --git-dir=/root/backend/.git --work-tree=/root/backend reset --hard origin/main
git --git-dir=/root/backend/.git --work-tree=/root/backend pull origin main
#################################################### 配置 ##############################################################
BASE_PATH=/root/backend
modelName=backend
profile=dev
JVM_MEMERY=" -Xms256M -Xmx256M -XX:MaxDirectMemorySize=256M"
# JVM_OPTION="${JVM_MEMERY} -XX:MetaspaceSize=64M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/backend/logs/heap_dump_${modelName}.hprof -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:/data0/logs/gc_${modelName}.log -server -Djava.security.egd=file:/dev/./urandom"
DES_PATH=/root/backend
JAR=${DES_PATH}/ruoyi-admin/target/ruoyi-admin.jar
LOG_BACK_LOG=/root/backend/logs/back/${modelName}-info.log
LOG=/root/backend/logs/${modelName}.log
SELF_IP=$(ifconfig | grep "10.0.0" | awk '{print $2}' | cut -d ':' -f 2 |head -n 1)
if [[ "${SELF_IP}" == "" ]] ;then
SELF_IP=$(ifconfig -a | grep -E '172.|10.|192.' | grep -E 'Bcast|broadcast' | grep -E 'Mask|netmask' | awk '{print $2}' | cut -d ':' -f 2 |head -n 1)
__kill() {
local process_name=$1
PID=$(ps -ef | grep "${process_name}.jar" | grep -v grep | awk '{print $2}' | xargs)
local sleep_seconds=10
local cur_sleep_second=1
if [[ -z "$PID" ]]; then
__green "进程 ${process_name} 未运行"
return 0
fi
#################################################### 构建 ############################################################
cd /root/backend && mvn clean && mvn install -T 4
pkill -f /root/backend/ruoyi-admin/target/ruoyi-admin.jar
nohup java ${JVM_OPTION} -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6011 -jar ${JAR} --spring.profiles.active=${profile} >> ${LOG} 2>&1 &
tail -f /root/backend/logs/backend.log
#################################################### 启动 ##############################################################
while [[ -n "$PID" && "$sleep_seconds" -ge "$cur_sleep_second" ]]; do
__green "尝试kill PID: ${PID}"
if [ "$sleep_seconds" -eq "$cur_sleep_second" ]; then
__red "强制关闭: ${cur_sleep_second}"
kill -9 $PID >> /dev/null 2>&1
else
kill $PID >> /dev/null 2>&1
fi
__green "停止程序计时: ${cur_sleep_second}"
sleep 1
cur_sleep_second="$(_math "$cur_sleep_second" + 1)"
PID=$(ps -ef | grep "${process_name}.jar" | grep -v grep | awk '{print $2}' | xargs)
done
if [[ -n "$PID" ]]; then
__red "无法停止进程 ${process_name}, PID: ${PID}"
return 1
else
__green "进程 ${process_name} 已成功停止"
return 0
fi
}
# 获取本机IP
__get_ip() {
local ip=$(ifconfig | grep "10.0.0" | awk '{print $2}' | cut -d ':' -f 2 | head -n 1)
if [[ -z "$ip" ]]; then
ip=$(ifconfig -a | grep -E '172.|10.|192.' | grep -E 'Bcast|broadcast' | grep -E 'Mask|netmask' | awk '{print $2}' | cut -d ':' -f 2 | head -n 1)
fi
echo "$ip"
}
# 创建日志目录
__create_log_dir() {
if [[ ! -d "$LOG_PATH" ]]; then
mkdir -p "$LOG_PATH"
__green "创建日志目录: $LOG_PATH"
fi
if [[ ! -d "$(dirname "$BACK_LOG")" ]]; then
mkdir -p "$(dirname "$BACK_LOG")"
__green "创建备份日志目录: $(dirname "$BACK_LOG")"
fi
}
# 主函数
main() {
# 加载环境变量
source /etc/profile
# 获取最新代码
__green "拉取最新代码..."
git --git-dir=${BASE_PATH}/.git --work-tree=${BASE_PATH} fetch origin main
git --git-dir=${BASE_PATH}/.git --work-tree=${BASE_PATH} reset --hard origin/main
git --git-dir=${BASE_PATH}/.git --work-tree=${BASE_PATH} pull origin main
# 构建项目
__green "开始构建项目..."
cd ${BASE_PATH} && mvn clean && mvn install -T 4
if [[ $? -ne 0 ]]; then
__red "Maven构建失败!"
exit 1
fi
# 检查JAR文件是否存在
if [[ ! -f "$JAR_PATH" ]]; then
__red "JAR文件不存在: $JAR_PATH"
exit 1
fi
# 停止现有进程
__green "停止现有进程..."
__kill "$MODEL_NAME"
# 创建日志目录
__create_log_dir
# 获取本机IP
SELF_IP=$(__get_ip)
__green "本机IP: $SELF_IP"
# 启动应用
__green "启动应用..."
nohup java ${JVM_OPTION} -jar ${JAR_PATH} --spring.profiles.active=${PROFILE} >> ${LOG_FILE} 2>&1 &
# 等待应用启动
sleep 3
# 检查进程是否启动成功
local new_pid=$(ps -ef | grep "${MODEL_NAME}.jar" | grep -v grep | awk '{print $2}')
if [[ -n "$new_pid" ]]; then
__green "应用启动成功! PID: $new_pid"
else
__red "应用启动失败!"
exit 1
fi
# 跟踪日志
__green "开始跟踪日志..."
tail -f ${LOG_FILE}
}
# 执行主函数
main "$@"