Files
spdm-backend/gateway2/src/main/resources/bin/start.sh
2025-10-15 16:16:37 +08:00

75 lines
2.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Spring Boot 网关项目启动脚本
JAR_PATH="/home/app/gateway2"
JAR_NAME="gateway2-0.0.1-SNAPSHOT.jar"
FULL_JAR_PATH="${JAR_PATH}/${JAR_NAME}"
# 与logback.xml保持一致的日志路径
LOG_HOME="/home/app/gateway2/logs"
LOG_FILE="${LOG_HOME}/running.log"
# JVM参数
JVM_OPTS="-Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${LOG_HOME}/heapdump.hprof"
# 默认为交互模式(显示实时日志)
silent_mode=0
# 处理参数:如果传入 --silent 则启用静默模式
if [ "$1" = "--silent" ]; then
silent_mode=1
fi
# 函数定义
check_jar_exists() {
if [ ! -f "${FULL_JAR_PATH}" ]; then
echo "ERROR: Jar包不存在路径${FULL_JAR_PATH}"
exit 1
fi
}
get_running_pid() {
ps -ef | grep "${JAR_NAME}" | grep -v "grep" | awk '{print $2}'
}
# 启动服务
check_jar_exists
PID=$(get_running_pid)
if [ -n "${PID}" ]; then
echo "项目已在运行中PID: ${PID}"
exit 0
fi
# 确保日志目录存在
if [ ! -d "${LOG_HOME}" ]; then
mkdir -p "${LOG_HOME}"
echo "日志目录不存在,已自动创建:${LOG_HOME}"
fi
echo "正在启动项目... 实时日志如下(按 Ctrl+C 可退出查看,进程会继续运行)"
echo "======================================================================"
# 启动项目,保留控制台输出
nohup java ${JVM_OPTS} -jar "${FULL_JAR_PATH}" > "${LOG_FILE}" 2>&1 &
# 获取刚启动的进程PID
NEW_PID=$(get_running_pid)
# 实时显示日志
if [ -n "${NEW_PID}" ]; then
# 根据模式决定是否显示实时日志
if [ ${silent_mode} -eq 0 ]; then
echo "实时日志如下(按 Ctrl+C 可退出查看,进程会继续运行)"
tail -f "${LOG_FILE}"
fi
# 无论 tail 命令如何退出,都认为启动成功
echo "======================================================================"
echo "项目启动成功PID: ${NEW_PID}"
echo "日志文件路径:${LOG_FILE}"
exit 0 # 强制返回成功状态
else
echo "======================================================================"
echo "项目启动失败!请查看日志:${LOG_FILE}"
exit 1
fi