SpringBoot启动脚本
# 启动脚本
#!/bin/bash
# 项目名称
APP_NAME="app"
# JAR 文件名
JAR_NAME="/data/www/${APP_NAME}/${APP_NAME}.jar"
# PID 文件
PID_FILE="/data/www/${APP_NAME}/${APP_NAME}.pid"
ENV=prod
JAVA_OPTS="-Xms512m -Xmx1g -XX:+UseConcMarkSweepGC"
# 启动应用
start() {
if [ -f $PID_FILE ]; then
echo "$APP_NAME is already running."
exit 1
fi
echo "Starting $APP_NAME..."
# nohup /usr/bin/java -jar $JAR_NAME --spring.profiles.active=$ENV >>/dev/null 2>&1 &
nohup /usr/bin/java $JAVA_OPTS -jar $JAR_NAME --spring.profiles.active=$ENV > /dev/null 2>&1 &
echo $! > $PID_FILE
echo "$APP_NAME started with PID $(cat $PID_FILE)."
}
# 停止应用
# 超时时间(秒)
STOP_TIMEOUT=30
# 停止函数
stop() {
if [ -f "$PID_FILE" ]; then
PID=$(cat $PID_FILE)
if ps -p $PID > /dev/null; then
echo "Stopping $APP_NAME (PID: $PID)..."
kill $PID
# 等待进程终止
SECONDS_WAITED=0
while ps -p $PID > /dev/null; do
if [ $SECONDS_WAITED -ge $STOP_TIMEOUT ]; then
echo "Process did not terminate within $STOP_TIMEOUT seconds, killing forcefully..."
kill -9 $PID
break
fi
sleep 1
SECONDS_WAITED=$((SECONDS_WAITED + 1))
done
rm -f $PID_FILE
echo "$APP_NAME stopped."
else
echo "No process found with PID: $PID. It may have already stopped."
rm -f $PID_FILE
fi
else
echo "PID file not found. Is $APP_NAME running?"
fi
}
# 重启应用
restart() {
stop
start
}
# 应用信息
info() {
if [ -f $PID_FILE ]; then
echo "$APP_NAME is running with PID $(cat $PID_FILE)."
else
echo "$APP_NAME is not running."
fi
}
# 检查输入的命令
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
info)
info
;;
*)
echo "Usage: $0 {start|stop|restart|info}"
exit 1
;;
esac
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
上次更新: 2024/08/31, 14:54:59