问题

在 CentOS/RHEL 7 系统中,配置文件 /etc/rc.d/rc.local 中的脚本/命令在启动时无法工作。
在早期的 CentOS/RHEL 版本中也可以使用。
它是否已弃用,或者是否有仍然使用此方法的解决方法?

变通办法

  1. 使用 systemd ,init 脚本不再存在。
    因此,启动时任务的执行必须改变。
    在 CentOS/RHEL 7 中,/etc/rc.d/rc.local 文件由 rc-local 服务控制。
...
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
  1. 默认情况下,文件/etc/rc.d/rc.local 没有执行权限。
    请将执行权限添加到此文件。
# ls -l /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 Nov  8 00:20 /etc/rc.d/rc.local
# chmod +x /etc/rc.d/rc.local
# ls -l /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Nov  8 00:20 /etc/rc.d/rc.local
  1. 启用rc.local 服务,确保每次重启后它都启动。
# systemctl enable rc-local

确认服务是否开启:

# systemctl status rc-local.service
  1. 然后,请启动 rc-local 服务。
# systemctl start rc-local
如何在 CentOS/RHEL 7 中使用 /etc/rc.d/rc.local 执行脚本/命令
on  it road.com

解决方法

CentOS/RHEL 7默认关闭rc.local服务,查看etc/rc.d/rc.local配置文件,有提示。

# cat /etc/rc.d/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
日期:2020-09-17 00:13:36 来源:oir作者:oir