#!/bin/bash

export deploy_path=/opt/webapps/bonus-material

# Define an array of JAR files to run
jars=("bonus-material.jar --spring.config.location=file:material_bootstrap.yml")

# 遍历数组并检查每个JAR文件的进程
for jar_with_args in "${jars[@]}"; do
    # 提取JAR文件名（不包括参数）
    jar=$(echo $jar_with_args | awk '{print $1}')

    # 检查进程是否启动
    echo "原应用程序1 $jar "
    pids=$(pgrep -f $jar || true)
    echo "原应用程序2 $jar "
    if [ -n "$pids" ]; then
        echo "$jar is running with PID(s): $pids"
        # 杀死进程
        for pid in $pids; do
            kill -9 $pid
            if [ $? -eq 0 ]; then
                echo "Process $pid for $jar killed successfully"
            else
                echo "Failed to kill process $pid for $jar"
            fi
        done
    else
        echo "$jar is not running"
    fi
done

cd $deploy_path || { echo "Failed to change directory to $deploy_path"; exit 1; }
# Iterate over the JAR files array
for jar in "${jars[@]}"; do
    echo "Starting $jar"
    nohup /usr/local/jdk1.8.0/bin/java -jar $jar >/dev/null 2>&1 &
done

echo "All JARs have been run successfully."

