使用 systemctl 管理 SpringBoot 应用

/etc/systemd/system/my-application.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[Unit]
Description=ApplicationName

[Service]
StartLimitInterval=5
StartLimitBurst=10
Restart=always
RestartSec=120
StandardOutput=append:/data/project/logs/stdout.log
StandardError=append:/data/project/logs/stderr.log
WorkingDirectory=/opt/project
ExecStart=/usr/local/java/jdk-21.0.3+9-jre/bin/java \
-XX:+ExitOnOutOfMemoryError \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/data/project/dumps/oom.bin \
-Dspring.profiles.active= \
-Dspring.config.location=classpath:application.yml,/opt/project/application.yml \
-Dlogging.config=/opt/project/logback.xml \
-jar /opt/project/project.jar

[Install]
WantedBy=multi-user.target
1
2
3
4
5
systemctl daemon-reload

systemctl status my-application.service
# 开机自启动
systemctl enable my-application.service