首先查看一下rc-local.service是如何处理脚本的

cat /lib/systemd/system/rc-local.service

可以看到调用的是/etc/rc.local,所以我们创建这个文件并进行编辑

vi /etc/rc.local

内容如下

#!/bin/sh -e

# 这里是需要开机执行的代码
/root/start.sh

exit 0

保存退出后修改权限为可执行

chmod +x /etc/rc.local

实际上

systemctl enable rc-local.service

是为了将

/lib/systemd/system/rc-local.service

的符号链接创建到

/etc/systemd/system

systemctl disable rc-local.service

实际上则是取消上面的链接,所以,修改文件要修改源文件

/lib/systemd/system/rc-local.service

如果设置开机启动提示下面的内容

大意是rc-local服务文件中没有Install字段的相关信息,如WantedByRequiredByAlsoAlias,如不写,系统就不认为它是一个服务

The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
settings in the [Install] section, and DefaultInstance for template units).
This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
instance name specified.

那么可以在服务配置文件中增加一个字段

vi /lib/systemd/system/rc-local.service

下面是具体内容

[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

# 这里是新增加的
[Install]
WantedBy=multi-user.target

查看开机启动项

service --status-all