把spring-boot项目配置为Linux systemd服务,并注册自启动

来源:互联网 发布:a5源码之家 编辑:程序博客网 时间:2024/05/20 04:15

把spring-boot项目配置为Linux systemd服务之后,可以方便地使用下面几个命令:

systemctl start myservcie.service
systemctl stop myservcie.service
systemctl restart myservcie.service
systemctl status myservcie.service

或这些:

service myservice status

service myservice start

service myservice stop

service myservice restart


1、开始配置:

vi /usr/lib/systemd/system/user-service.service
[Unit]Description=My ServiceAfter=syslog.target[Service]ExecStart=/usr/java/default/bin/java -jar /opt/app/spring-boot/myservice.jar --spring.profiles.active=devSuccessExitStatus=143[Install]WantedBy=multi-user.target
其中,ExecStart可以加上其他参数,比如:
/usr/java/default/bin/java -jar -Xms256m -Xmx4G  -Dlog.level.console=warn -Dlog.level.ysb=info /opt/app/spring-boot/myservice.jar --spring.profiles.active=prod
另外,143是spring-boot服务被stop的时候的status code:
# service myservice stop
# service myservice status
myservice.service - My Service
   Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled)
   Active: inactive (dead) since Tue 2016-11-29 14:21:19 CST; 1s ago
  Process: 19146 ExecStart=/usr/java/default/bin/java -jar /opt/app/spring-boot/myservice.jar --spring.profiles.active=dev(code=exited, status=143)
 Main PID: 19146 (code=exited, status=143)

如果不加上SuccessExitStatus=143,stop服务的时候会变成failed状态,而不是inactive状态。

2、注册开机自启动:
systemctl enable myservice.service

如果修改服务配置文件,需要执行:
systemctl daemon-reload

3、查看该服务的console log:
journalctl -umyservice.service
journalctl -f -umyservice.service


0 0
原创粉丝点击