Files
hzdyjs/backend/start-ruoyi-monitor.sh
T
2025-08-08 20:35:17 +08:00

245 lines
5.9 KiB
Bash
Executable File
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
# =============================================================================
# RuoYi Monitor Admin 监控服务启动脚本 (CentOS)
# 用途: 启动、停止、重启、查看状态 ruoyi-monitor-admin.jar
# 端口: 9090 (监控中心)
# =============================================================================
# 配置变量
APP_NAME="ruoyi-monitor-admin"
JAR_NAME="ruoyi-monitor-admin.jar"
JAR_PATH="/opt/ruoyi/monitor"
JAR_FILE="$JAR_PATH/$JAR_NAME"
PID_FILE="/var/run/${APP_NAME}.pid"
LOG_DIR="/var/log/ruoyi"
LOG_FILE="$LOG_DIR/${APP_NAME}.log"
JVM_OPTS="-Xms256m -Xmx512m -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_DIR"
SPRING_PROFILES="prod"
# 颜色配置
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 创建必要目录
create_dirs() {
sudo mkdir -p "$LOG_DIR"
sudo mkdir -p "$(dirname "$PID_FILE")"
sudo chown -R $(whoami):$(whoami) "$LOG_DIR"
}
# 检查jar文件是否存在
check_jar() {
if [ ! -f "$JAR_FILE" ]; then
echo -e "${RED}错误: JAR文件不存在: $JAR_FILE${NC}"
echo -e "${YELLOW}请确保已将jar包放置到: $JAR_PATH${NC}"
exit 1
fi
}
# 获取进程ID
get_pid() {
if [ -f "$PID_FILE" ]; then
cat "$PID_FILE"
else
ps -ef | grep "$JAR_NAME" | grep -v grep | awk '{print $2}'
fi
}
# 检查服务状态
is_running() {
local pid=$(get_pid)
if [ -n "$pid" ]; then
if ps -p "$pid" > /dev/null 2>&1; then
return 0
else
# PID文件存在但进程不存在,清理PID文件
[ -f "$PID_FILE" ] && rm -f "$PID_FILE"
return 1
fi
else
return 1
fi
}
# 启动服务
start() {
echo -e "${BLUE}==================== 启动 $APP_NAME ====================${NC}"
check_jar
create_dirs
if is_running; then
local pid=$(get_pid)
echo -e "${YELLOW}$APP_NAME 已经在运行中,PID: $pid${NC}"
return 1
fi
echo -e "${GREEN}正在启动 $APP_NAME...${NC}"
echo "JAR文件: $JAR_FILE"
echo "日志文件: $LOG_FILE"
echo "JVM参数: $JVM_OPTS"
echo "Spring环境: $SPRING_PROFILES"
# 启动应用
nohup java $JVM_OPTS \
-jar "$JAR_FILE" \
--spring.profiles.active="$SPRING_PROFILES" \
--logging.file.name="$LOG_FILE" \
> "$LOG_FILE" 2>&1 &
local pid=$!
echo $pid > "$PID_FILE"
# 等待启动
echo -e "${YELLOW}等待应用启动...${NC}"
sleep 3
if is_running; then
echo -e "${GREEN}✓ $APP_NAME 启动成功!${NC}"
echo "PID: $(get_pid)"
echo "端口: 9090"
echo "日志: tail -f $LOG_FILE"
echo "访问: http://localhost:9090/admin"
echo "说明: 这是Spring Boot Admin监控中心"
else
echo -e "${RED}✗ $APP_NAME 启动失败!${NC}"
echo "请查看日志: tail -f $LOG_FILE"
exit 1
fi
}
# 停止服务
stop() {
echo -e "${BLUE}==================== 停止 $APP_NAME ====================${NC}"
if ! is_running; then
echo -e "${YELLOW}$APP_NAME 未运行${NC}"
return 1
fi
local pid=$(get_pid)
echo -e "${GREEN}正在停止 $APP_NAME,PID: $pid${NC}"
# 优雅停止
kill "$pid"
# 等待进程停止
local count=0
while is_running && [ $count -lt 30 ]; do
sleep 1
count=$((count + 1))
echo -n "."
done
echo
if is_running; then
echo -e "${YELLOW}优雅停止超时,强制终止进程...${NC}"
kill -9 "$pid"
sleep 2
fi
if is_running; then
echo -e "${RED}✗ $APP_NAME 停止失败!${NC}"
exit 1
else
echo -e "${GREEN}✓ $APP_NAME 已停止${NC}"
[ -f "$PID_FILE" ] && rm -f "$PID_FILE"
fi
}
# 重启服务
restart() {
echo -e "${BLUE}==================== 重启 $APP_NAME ====================${NC}"
stop
sleep 2
start
}
# 查看状态
status() {
echo -e "${BLUE}==================== $APP_NAME 状态 ====================${NC}"
if is_running; then
local pid=$(get_pid)
echo -e "${GREEN}✓ $APP_NAME 正在运行${NC}"
echo "PID: $pid"
echo "端口: 9090"
echo "内存使用:"
ps -p "$pid" -o pid,ppid,etime,pmem,pcpu,comm --no-headers
echo "网络端口:"
netstat -tlnp 2>/dev/null | grep ":9090 " || echo "端口9090未监听"
else
echo -e "${RED}✗ $APP_NAME 未运行${NC}"
fi
echo "最近日志:"
if [ -f "$LOG_FILE" ]; then
tail -10 "$LOG_FILE"
else
echo "日志文件不存在: $LOG_FILE"
fi
}
# 查看日志
logs() {
echo -e "${BLUE}==================== $APP_NAME 日志 ====================${NC}"
if [ -f "$LOG_FILE" ]; then
tail -f "$LOG_FILE"
else
echo -e "${RED}日志文件不存在: $LOG_FILE${NC}"
fi
}
# 显示帮助信息
usage() {
echo -e "${BLUE}RuoYi Monitor Admin 监控服务管理脚本${NC}"
echo "用法: $0 {start|stop|restart|status|logs}"
echo ""
echo "命令说明:"
echo " start - 启动服务"
echo " stop - 停止服务"
echo " restart - 重启服务"
echo " status - 查看状态"
echo " logs - 查看实时日志"
echo ""
echo "配置信息:"
echo " 应用名称: $APP_NAME"
echo " JAR文件: $JAR_FILE"
echo " PID文件: $PID_FILE"
echo " 日志文件: $LOG_FILE"
echo " 服务端口: 9090"
echo " 访问地址: http://localhost:9090/admin"
echo ""
echo "服务说明:"
echo " 这是Spring Boot Admin监控中心,用于监控主应用的运行状态"
echo " 需要在主应用启动后再启动此服务"
}
# 主逻辑
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
logs)
logs
;;
*)
usage
exit 1
;;
esac
exit 0