java -jar配置系统服务

#nohup java -jar test-project.jar &

项目环境

[root@appdockerenv project]# pwd
/project
[root@appdockerenv project]# ll
-rw-r--r--. 1 root root 1112 9月 3 10:31 application.yml
-rw-r--r--. 1 root root 169367250 9月 3 14:46 test-project.jar
-rw-r--r--. 1 root root 1756 7月 21 14:23 logback.xml
drwxr-xr-x. 3 root root 20 9月 3 10:23 logs
-rw-------. 1 root root 2115 9月 3 14:43 nohup.out
-rw-r--r--. 1 root root 76 9月 3 13:47 stop.sh
-rwxr-xr-x. 1 root root 182 9月 3 14:45 start.sh

启动脚本 ​​start.sh​

#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el7_8.x86_64
export PATH=$JAVA_HOME/bin:$PATH
CMD_PATH="/project"
cd $CMD_PATH
nohup java -jar test-project.jar &
echo $! > $CMD_PATH/project.pid

关闭脚本 ​​stop.sh​

#!/bin/bash
CMD_PATH="/project"
PID=$(cat $CMD_PATH/project.pid)
kill -9 $PID

新建 project.service

# Systemd unit file for jar service
[Unit]
Description=XXX System
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el7_8.x86_64
ExecStart=/safpot/start.sh test-project
ExecStop=/safpot/stop.sh test-project
PrivateTmp=true

[Install]
WantedBy=multi-user.target

服务管理

systemctl stop project
systemctl start project
systemctl enable project
发表评论

相关文章