ubuntu 20.04通过systemd方式增加开机启动

 

说在前面:

 在ubuntu 20.04上是没有像centos 那样的/etc/rc.local文件的。如果要加开机启动,可以通过systemd的方式。

方式如下

1. 在 /etc/systemd/system 文件下新建一个文件xx.service

2. 编辑内容

 

[Unit]
Description=xx app init service
Documentation=see /opt/init.sh
After=network.target auditd.service

[Service]
ExecStart=/bin/bash /opt/init.sh
Restart=on-failure
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Alias=tsingyun.service

这里需要注意2点

   1. Type=oneshot ,这种方式可以让你用nohup 起动某些服务,比如spring boot 之类

   2. RemainAfterExit=yes 表示虽然nohup后台运行了,也认为这个服务是起来的。可以通过systemctl status xx.service 查看状态

3. 要启动的程序可以写在/opt/init.sh中

例如

#!/bin/bash
# 开机启动脚本,一定要后台运行。java程序要加nohup
export JAVA_HOME=/opt/jdk1.8.0_281
export PATH=$PATH:$JAVA_HOME/bin
/opt/apache-activemq-5.15.15/bin/activemq start

 

4. 

systemctl enable xx.service

systemctl restart xx.service 

 

参考文档

http://manpages.ubuntu.com/manpages/focal/man5/systemd.service.5.html

 

 

posted on 2021-05-12 15:25  cococooder  阅读(1300)  评论(1编辑  收藏  举报