新建开机自启脚本 /usr/lib/systemd/system/xxx.service

#!/bin/sh

[Unit]
Description=xxxService
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/app/springboot/xxx-start.sh
ExecStop=/app/springboot/xxx-stop.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

新建一个启动脚本  /apps/springboot/xxx-start.sh

#!/bin/sh

export JAVA_HOME=/usr/local/software/jdk
export PATH=$JAVA_HOME/bin:$PATH

nohup java -jar /app/springboot/xxx.jar > /var/logs/xxx-service.log 2>&1 & echo $! > /var/run/xxx-service.pid


新建一个停止脚本 /apps/springboot/xxx-stop.sh

#!/bin/sh

PID=$(cat /var/run/xxx-service.pid)
kill -9 $PID

增加权限

chmod +x /app/springboot/xxx-start.sh
chmod +x /app/springboot/xxx-stop.sh

开机启动

chkdonfig xxx.service on
或者
systemctl enable xxx.service