Ubuntu 22.04 中rc.local 服务并没有启用,手动配置的步骤如下
1、检查rc-local.service服务是否已经配置开机启动
root@mydbserver:~# systemctl is-enabled rc-local.service
static
2、rc-local.service文件增加2行
root@mydbserver:~# vim /lib/systemd/system/rc-local.service
[Install]
WantedBy=multi-user.target
3、检查rc-local.service服务是否已经配置开机启动
root@mydbserver:~# systemctl is-enabled rc-local.service
disabled
4、对rc-local.service服务配置开机启动,并且启动它
root@mydbserver:~# systemctl enable rc-local.service
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /lib/systemd/system/rc-local.service.
root@mydbserver:~# systemctl is-enabled rc-local.service
enabled
root@mydbserver:~# systemctl start rc-local.service
root@mydbserver:~# systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
Active: inactive (dead)
Condition: start condition failed at Mon 2024-07-01 15:20:53 CST; 23s ago
└─ ConditionFileIsExecutable=/etc/rc.local was not met
Docs: man:systemd-rc-local-generator(8)
Jul 01 15:20:53 wcslsf5t9hm3 systemd[1]: Condition check resulted in /etc/rc.local Compatibility being skipped.
root@mydbserver:~# vim /etc/rc.local
#!/bin/bash
root@mydbserver:~# sudo chmod 777 /etc/rc.local
root@mydbserver:~# systemctl start rc-local.service
root@mydbserver:~# systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (exited) since Mon 2024-07-01 15:25:16 CST; 4s ago
Docs: man:systemd-rc-local-generator(8)
Process: 2618076 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Jul 01 15:25:16 wcslsf5t9hm3 systemd[1]: Starting /etc/rc.local Compatibility...
Jul 01 15:25:16 wcslsf5t9hm3 systemd[1]: Started /etc/rc.local Compatibility.
5、之后就可以直接在/etc/rc.local文件中添加可执行的脚本文件,开机调用这个可执行的脚本文件