Ubuntu18.04 设置开机自启动服务

Ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。

1、编辑rc-local.service文件

sudo vi /etc/systemd/system/rc-local.service

2、在rc-local.service文件中加入以下内容

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

一般启动文件需要三个组成部分

[Unit]段: 启动顺序与依赖关系

[Service] 段: 启动行为,如何启动,启动类型

[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动

3、创建文件rc.local ,Ubuntu-18.04 默认是没有 /etc/rc.local 这个文件的,需要自己创建

sudo vi /etc/rc.local

4、将下列内容复制进rc.local文件

#!/bin/sh -e
#
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.
sudo echo "在这里行写入你需自启动服务的脚本" > /usr/local/text.log
exit 0

5、给rc.local加上权限

sudo chmod +x /etc/rc.local

6、启用服务

sudo systemctl enable rc-local

7、启动服务并检查状态

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service

8、重启并检查test.log文件

cat /usr/local/test.log

  

猜你喜欢

转载自www.cnblogs.com/VicLiu/p/12470245.html