#!/bin/bash

# 设置 JAR 文件名称
JAR_FILE="tio-site-all.jar"

# 查找正在运行的进程并获取其 PID
PID=$(ps aux | grep "$JAR_FILE" | grep -v grep | awk '{print $2}')

# 如果找到了进程，则杀死它
if [ ! -z "$PID" ]; then
    echo "Found process with PID: $PID. Stopping it..."
    kill $PID

    # 等待进程完全结束
    sleep 2

    # 如果进程仍然存在，则强制杀死
    if ps -p $PID > /dev/null; then
        echo "Process didn't stop. Forcing termination..."
        kill -9 $PID
    else
        echo "Process stopped successfully."
    fi
else
    echo "No running process found for $JAR_FILE."
fi

# 重新启动应用
echo "Restarting $JAR_FILE..."
nohup java -jar $JAR_FILE > tio-site-all.log 2>&1 &

echo "$JAR_FILE has been restarted."