linux 用脚本启动rocketmq控制台会被卡住
先给脚本设置权限
chmod +x manage-services.sh
#!/bin/bash
# 基础配置
SRC_DIR=/data/HH-Cloud # 源码路径
WORK_DIR=/data/jar # 工作目录
LOG_DIR=/data/scripts/logs # 日志目录
# 服务脚本列表
SERVICE_SCRIPTS=(
"xx-gateway"
)
# Nacos、RocketMQ 和 Seata 启动和停止脚本
NACOS_START_CMD="bash /data/nacos/bin/startup.sh -m standalone"
NACOS_STOP_CMD="bash /data/nacos/bin/shutdown.sh"
ROCKETMQ_START_CMD="nohup sh /data/rocketmq-5.2.0/bin/mqnamesrv > /data/logs/rocketmq_namesrv.log 2>&1 &"
ROCKETMQ_BROKER_CMD="nohup sh /data/rocketmq-5.2.0/bin/mqbroker -n 127.0.0.1:9876 > /data/logs/rocketmq_broker.log 2>&1 &"
ROCKETMQ_STOP_CMD="sh /data/rocketmq-5.2.0/bin/mqshutdown namesrv && sh /data/rocketmq-5.2.0/bin/mqshutdown broker"
SEATA_START_CMD="bash /data/seata-server-1.6.0/bin/seata-server.sh"
SEATA_STOP_CMD="bash /data/seata-server-1.6.0/bin/seata-server.sh stop"
OPTIONS="-Xms512m -Xmx512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
# 确保目录存在
mkdir -p $WORK_DIR
mkdir -p $LOG_DIR
# 启动单个服务
start_service() {
local SERVICE=$1
local JAR_PATH=""
local LOG_FILE=$LOG_DIR/$SERVICE.log
local PID_FILE=/var/run/$SERVICE.pid
echo "Starting $SERVICE..."
# 查找 JAR 文件
for DIR in "$SRC_DIR/hh-gateway/target" "$SRC_DIR/hh-modules/$SERVICE/target" "$SRC_DIR/hh-visual/$SERVICE/target" "$SRC_DIR/hh-auth/target"; do
if [ -f "$DIR/$SERVICE.jar" ]; then
JAR_PATH="$DIR/$SERVICE.jar"
break
fi
done
if [ -z "$JAR_PATH" ]; then
echo "Error: JAR file for $SERVICE not found!"
return
fi
# 检查是否已在运行
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
if kill -0 $PID 2>/dev/null; then
echo "$SERVICE is already running (PID: $PID)"
return
else
echo "Stale PID file found for $SERVICE. Removing it."
rm -f $PID_FILE
fi
fi
# 停止旧进程(如果未正确停止)
OLD_PID=$(ps -ef | grep "$SERVICE".jar | grep -v grep | awk '{print $2}')
if [ -n "$OLD_PID" ]; then
echo "Stopping old instance of $SERVICE (PID: $OLD_PID)..."
kill -9 $OLD_PID
echo "Old instance stopped."
fi
# 启动新服务
nohup java $OPTIONS -jar $JAR_PATH --spring.profiles.active=stage > $LOG_FILE 2>&1 &
NEW_PID=$!
echo $NEW_PID > $PID_FILE
echo "$SERVICE started (PID: $NEW_PID)"
}
# 停止单个服务
stop_service() {
local SERVICE=$1
local PID_FILE=/var/run/$SERVICE.pid
echo "Stopping $SERVICE..."
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
if kill -0 $PID 2>/dev/null; then
kill -9 $PID
rm -f $PID_FILE
echo "$SERVICE stopped."
else
echo "PID $PID not valid. Removing stale PID file."
rm -f $PID_FILE
fi
else
echo "$SERVICE is not running."
fi
}
# 查询单个服务状态
status_service() {
local SERVICE=$1
local PID_FILE=/var/run/$SERVICE.pid
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
echo "$SERVICE is running (PID: $(cat $PID_FILE))"
else
echo "$SERVICE is not running."
fi
}
# 启动 Nacos、RocketMQ、Seata 服务
start_nacos() {
echo "Starting Nacos..."
$NACOS_START_CMD &
echo "Nacos started."
}
start_rocketmq() {
echo "Starting RocketMQ..."
# 启动 NameServer
eval $ROCKETMQ_START_CMD
sleep 2 # 稍等一下,确保 NameServer 启动
# 启动 Broker
eval $ROCKETMQ_BROKER_CMD
sleep 2 # 稍等一下,确保 Broker 启动
# 使用 disown 完全将进程与终端分离
disown
echo "RocketMQ started. Logs are available in /data/logs/rocketmq_namesrv.log and /data/logs/rocketmq_broker.log"
}
start_seata() {
echo "Starting Seata..."
$SEATA_START_CMD
sleep 3 # 给 Seata 一些时间启动
echo "Seata started."
}
# 停止 Nacos、RocketMQ、Seata 服务
stop_nacos() {
echo "Stopping Nacos..."
$NACOS_STOP_CMD
echo "Nacos stopped."
}
stop_rocketmq() {
echo "Stopping RocketMQ..."
$ROCKETMQ_STOP_CMD
echo "RocketMQ stopped."
}
stop_seata() {
echo "Stopping Seata..."
$SEATA_STOP_CMD
echo "Seata stopped."
}
# 检查 Nacos 服务状态
status_nacos() {
if pgrep -f "nacos-server.jar" > /dev/null; then
echo "Nacos is running."
else
echo "Nacos is not running."
fi
}
# 检查 RocketMQ 服务状态
status_rocketmq() {
if pgrep -f "mqnamesrv" > /dev/null && pgrep -f "mqbroker" > /dev/null; then
echo "RocketMQ is running."
else
echo "RocketMQ is not running."
fi
}
# 检查 Seata 服务状态
status_seata() {
if pgrep -f "seata-server" > /dev/null; then
echo "Seata is running."
else
echo "Seata is not running."
fi
}
# 检查参数
if [ $# -lt 1 ]; then
echo "Usage: $0 <start|stop|status> [service_name]"
exit 1
fi
ACTION=$1
SERVICE=$2
# 根据参数执行操作
case $ACTION in
start)
if [ -z "$SERVICE" ]; then
# 启动所有服务
start_nacos
start_rocketmq
start_seata
for SERVICE in "${SERVICE_SCRIPTS[@]}"; do
start_service $SERVICE
sleep 2 # 确保每个服务有足够时间启动
done
echo "All services started."
else
# 启动指定服务
if [[ "$SERVICE" == "nacos" ]]; then
start_nacos
elif [[ "$SERVICE" == "rocketmq" ]]; then
start_rocketmq
elif [[ "$SERVICE" == "seata" ]]; then
start_seata
elif [[ " ${SERVICE_SCRIPTS[@]} " =~ " ${SERVICE} " ]]; then
start_service $SERVICE
else
echo "Invalid service name: $SERVICE"
exit 1
fi
fi
;;
stop)
if [ -z "$SERVICE" ]; then
# 停止所有服务
stop_nacos
stop_rocketmq
stop_seata
for SERVICE in "${SERVICE_SCRIPTS[@]}"; do
stop_service $SERVICE
done
echo "All services stopped."
else
# 停止指定服务
if [[ "$SERVICE" == "nacos" ]]; then
stop_nacos
elif [[ "$SERVICE" == "rocketmq" ]]; then
stop_rocketmq
elif [[ "$SERVICE" == "seata" ]]; then
stop_seata
elif [[ " ${SERVICE_SCRIPTS[@]} " =~ " ${SERVICE} " ]]; then
stop_service $SERVICE
else
echo "Invalid service name: $SERVICE"
exit 1
fi
fi
;;
status)
if [ -z "$SERVICE" ]; then
# 查询所有服务状态
echo "Checking Nacos status..."
status_nacos
echo "Checking RocketMQ status..."
status_rocketmq
echo "Checking Seata status..."
status_seata
for SERVICE in "${SERVICE_SCRIPTS[@]}"; do
status_service $SERVICE
done
else
# 查询指定服务状态
if [[ "$SERVICE" == "nacos" ]]; then
status_nacos
elif [[ "$SERVICE" == "rocketmq" ]]; then
status_rocketmq
elif [[ "$SERVICE" == "seata" ]]; then
status_seata
elif [[ " ${SERVICE_SCRIPTS[@]} " =~ " ${SERVICE} " ]]; then
status_service $SERVICE
else
echo "Invalid service name: $SERVICE"
exit 1
fi
fi
;;
*)
echo "Invalid action: $ACTION. Use 'start', 'stop', or 'status'."
exit 1
;;
esac