linux使用脚本启动java

配置文件内容

#!/bin/bash

# 定义变量
JAR_NAME="thingstream.jar"  # 替换为你的jar文件名
JAR_PATH="/eventCenter"
NEW_JAR_PATH="/tmp/${JAR_NAME}"

# 检查并杀掉已启动的JAR进程
echo "Checking for running JAR process..."
PID=$(ps aux | grep "${JAR_NAME}" | grep -v grep | awk '{print $2}')
if [ -n "$PID" ]; then
    echo "Found running JAR process with PID: ${PID}. Killing it..."
    kill -9 $PID
    sleep 2  # 等待2秒以确保进程已被终止
else
    echo "No running JAR process found."
fi

# 移除旧的JAR文件
if [ -f "${JAR_PATH}/${JAR_NAME}" ]; then
    echo "Removing old JAR file..."
    rm -f "${JAR_PATH}/${JAR_NAME}"
fi

# 将新的JAR文件移到目标位置
if [ -f "${NEW_JAR_PATH}" ]; then
    echo "Moving new JAR file to ${JAR_PATH}..."
    mv "${NEW_JAR_PATH}" "${JAR_PATH}/"
else
    echo "New JAR file not found at ${NEW_JAR_PATH}. Exiting..."
    exit 1
fi

# 定义一些变量
LOG_FILE="$SCRIPT_PATH/log.txt"

# 执行另一个脚本,并将其输出写入日志文件
"$JAR_PATH/eventCenter.sh" > "$LOG_FILE" 2>&1

# 检查上一个命令的状态码
if [ $? -eq 0 ]; then
    echo "Script executed successfully."
else
    echo "Error occurred while executing script."
fi

echo "Deployment completed."