Files
server/deploy-java.sh
2025-11-02 19:34:16 +08:00

56 lines
1.6 KiB
Bash
Raw Permalink 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
# 项目根目录
PROJECT_DIR="/root/xiaozhi-esp32-server-java"
TARGET_DIR="$PROJECT_DIR/target"
# 自动查找target目录中的JAR文件
JAR_FILE=$(find "$TARGET_DIR" -name "*.jar" -not -name "*.original" -type f | head -1)
if [ -z "$JAR_FILE" ]; then
echo "错误: 在 $TARGET_DIR 目录中未找到JAR文件"
echo "请先运行 'mvn clean package' 构建项目"
exit 1
fi
# 获取JAR文件名用于进程查找
APP_NAME=$(basename "$JAR_FILE")
# 提取应用名称前缀("-"之前的部分)用于进程匹配
APP_PREFIX=$(echo "$APP_NAME" | cut -d'-' -f1)
echo "找到JAR文件: $JAR_FILE"
echo "应用前缀: $APP_PREFIX"
echo "正在停止相关的 $APP_PREFIX 进程..."
# 查找并杀死相关进程(使用应用前缀匹配)
PID=$(ps -ef | grep "$APP_PREFIX" | grep "\.jar" | grep -v grep | awk '{print $2}')
if [ -n "$PID" ]; then
echo "找到进程 PID: $PID, 正在停止..."
kill $PID
sleep 5
# 检查进程是否已停止,如果未停止则强制杀死
if ps -p $PID > /dev/null; then
echo "进程仍在运行,强制杀死..."
kill -9 $PID
sleep 2
fi
echo "服务已停止"
else
echo "未找到运行中的服务"
fi
echo "正在启动 $APP_NAME ..."
nohup java -jar "$JAR_FILE" > output.log 2>&1 &
# 检查是否启动成功
NEW_PID=$!
sleep 3
if ps -p $NEW_PID > /dev/null; then
echo "服务启动成功! PID: $NEW_PID"
else
echo "服务启动可能失败,请检查日志"
fi