【问题解决】编写service文件配置java开机自启,启动脚本正常,service无效

问题描述

部署springboot工程到服务器环境,由于不能使用docker,进行原生部署。部署后编写启动脚本用于服务启动,然后编写service配置文件,添加到系统服务方便配置开机自启,但是开机自启无效,通过启动脚本可以正常启动文件

部署过程

构建jar文件并放到服务器

jar文件部署目录为/home/deploy/host.jar

编写启动脚本

/home/deploy/start.sh

#!/bin/bash
nohup java -jar /home/deploy/host.jar > /dev/null 2>&1 &
# 已经写了日志了,就把输出丢/dev/null了

执行启动脚本进行验证

ps -aux | grep java | grep /home/deploy/host.jar

脚本没问题,服务是正常启动了,kill掉,然后去写service

编写service

[Unit]
Description=my java service
After=network.target 

[Service]
ExecStart=/home/deploy/start.sh

[Install]
WantedBy=multi-user.target

通过service启动

systemctl start my-java-service

命令执行正常,但是服务并未启动

排查问题

systemctl status my-java-service

服务状态是inactivate(dead)

The unit my-java-service.service has successfully entered the 'dead' state.

journalctl -xef -u my-java-service

Main process exited, code=exited, status=1/FAILURE

Failed with result 'exit-code'.

the unit my-java-service.service has entered the 'failed' state with result 'exit-code'.

解决问题

主进程退出了是关键的错误信息,然后就发现service脚本有问题,Type默认是simple,而我们执行的脚本执行后就会退出,真正工作的是java的进程,所以要指定为forking,主进程结束后子进程会变成主

[Unit]
Description=my java service
After=network.target 

[Service]
Type=forking
ExecStart=/home/deploy/start.sh

[Install]
WantedBy=multi-user.target

验证

服务可以正常启动了

添加自启

systemctl enable my-java-service

额外提醒

每次修改service文件之后最好都reload一下

systemctl daemon-reload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码司机

源于社区,回馈社区,致敬技术

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值