为应用建立一个service文件
my-webapp.service
[Unit]
Description=SpringBoot REST Service
[Service]
User=ubuntu
WorkingDirectory=/usr/local/src/workspace/my-webapp
#要执行程序的路径
#调用jar文件的shell执行文件
fileExecStart=/usr/local/src/workspace/my-webapp/start.sh
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
保存在/etc/systemd/system目录下
创建脚本文件start.sh
#!/bin/sh
sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar
并授予可执行权限
sudo chmod u+x start.sh
启动服务
sudo systemctl daemon-reload
sudo systemctl enable my-webapp.service
sudo systemctl start my-webapp
sudo systemctl status my-webapp
停止服务
sudo systemctl stop my-webapp