linux使用脚本启动java

配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/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."