1.打包可执行jar文件
(1)打包需要java -jar demo.jar的包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(2)打包可执行二进制包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
2.编写管理jar执行脚本
(1)start.sh
#!/bin/bash
nohup java -jar /opt/tomcat/test/demo.jar > nohup-demo.log 2>&1 &
echo 'demo.jar is runing'
(2)stop.sh
#!/bin/bash
pid=`ps -ef | grep demo.jar | grep -v grep | awk '{print $2}'`
if [ -z "${pid}" ]; then
echo "demo.jar is not running"
else
echo "kill thread...$pid"
kill -9 $pid
fi
(3)restart.sh
#!/bin/bash
/opt/tomcat/test/stop.sh;
/opt/tomcat/test/start.sh;
3.编写service脚本,放在/etc/systemd/system下面
(1)demo.service
[Unit]
Description=demo.jar测试
After=syslog.target network.target remote-fs.target nss-lookup.target
StartLimitIntervalSec=0
[Service]
Type=forking
Restart=always
RestartSec=10
ExecStart=/opt/tomcat/test/start.sh
ExecReload=/opt/tomcat/test/restart.sh
ExecStop=/opt/tomcat/test/stop.sh
[Install]
WantedBy=multi-user.target
其中以下配置,是让服务如果意外停止后,进行重启
#突破重启次数限制
StartLimitIntervalSec=0
#永远重启
Restart=always
#发生停止后10秒后重启
RestartSec=10
4.测试
(1)正常的启动方式
service demo start (或者systemctl start demo.service) service demo stop (或者systemctl stop demo.service) service demo restart (或者systemctl restart demo.service)
(2)错误排查systemctl status demo.service -l
1.举例下面这个问题及解决办法
在启动脚本中指定jdk的环境变量,或者使用命令对java做软链接
ln -s /usr/local/jdk/bin/java /sbin/java
本文由 GY 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2023/04/27 06:53