【Linux问题】解决rc.local文件不存在问题(Ubuntu 18.04)

        ubuntu16.04 版本后就不再使用 initd 管理系统,而改用 systemd。

解决方法:
第一步:编写 rc-local.service 文件。

        默认的 service文件都是存在与 /etc/systemd/system 目录下,有点像某种服务的配置文件。注意到 /lib/systemd/system 下也有个 rc-local.service,可以借用这个模板来进行修改,也可以从头开始编写。

sudo vim /etc/systemd/system/rc-local.service
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local
[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99
[Install]
 WantedBy=multi-user.target

        其中 Unit 字段主要描述服务的启动顺序以及依赖关系,Service 字段主要描述如何启动,Install 字段描述如何安装这个服务。

第二步:激活 rc-local.service。

sudo systemctl enable rc-local.service

第三步:手动创建 /etc/rc.local,并赋予执行权限。

        ubuntu 18.04 系统默认已经将 /etc/rc.local 文件移除了,因此,我们需要手动创建一个,并将需要开机执行的命令写入到文件中,  

sudo vim /etc/rc.local
#!/bin/bash
# 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.

# 下面填入开机启动的命令
# ......

exit 0
sudo chmod +x /etc/rc.local

 第四步:启动这个服务并查看它的状态。

sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

 

第五步:reboot 重启验证。

猜你喜欢

转载自blog.csdn.net/weixin_48896613/article/details/127208376
今日推荐