LOCK_FILE="/www/wwwroot/im1.20633.cn/service_restart.lock"

if [ -f $LOCK_FILE ]; then
    echo "任务正在执行中，退出..."
    exit 0
fi

touch $LOCK_FILE

# 设置接口URL
URL="http://localhost:6063/mytio/config/checkServer.tio_x"  # 替换为你实际的接口地址

# 设置请求超时时间为1秒
TIMEOUT=4

# 使用 curl 发起请求，判断是否超时
curl --max-time $TIMEOUT $URL > /dev/null 2>&1

# 判断 curl 的退出码
if [ $? -ne 0 ]; then
    echo "请求超时，正在重启 Jar 服务..."
    # 停止当前运行的 Jar 服务

    # nohup java -jar /path/to/your-jar-file.jar > /dev/null 2>&1 &
    JAR_FILE="tio-site-all.jar"

    # 查找正在运行的进程并获取其 PID
    PID=$(ps aux | grep "$JAR_FILE" | grep -v grep | awk '{print $2}')
    
    # 如果找到了进程，则杀死它
    if [ ! -z "$PID" ]; then
        echo "printf thread log..."
        
        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 /usr/bin/java -jar "/www/wwwroot/im1.20633.cn/tio-site-all.jar" >> "/www/wwwroot/im1.20633.cn/log_$(date +\%Y\%m\%d_\%H\%M\%S).log" 2>&1 &
       # 等待进程重启结束
    sleep 2
    echo "$JAR_FILE has been restarted."
else
    echo "success"
fi

rm -f $LOCK_FILE