以前在 Redhat 及 CentOS 要设定开机自动执行的 Shell Script, 一般都会在档案 /etc/rc.d/rc.local 加入需要执行的 Shell Script 或指令, 但在 CentOS 7 开始, /etc/rc.d/rc.local 默认权限改为 644, 即没有执行权限, 为什么会这样, 可以开启 CentOS 7 的 /etc/rc.d/rc.local 看看, 里面有 Redhat 的说明:
|
1 2 3 4 5 6 7 8 9 10 11 |
#!/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 constrast 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. |
可以看到这个档案在 RHEL 及 CentOS 7 只为了解决兼容性, Redhat 建议还是自行建立 systemd 服务或者 udev rules 较好, 如果真的需要使用 rc.local, 只要执行 chmod 给予 rc.local 可执行权限即可:
# chmod +x /etc/rc.d/rc.local
执行以上指令后, 下次开机便会自动执行 rc.local 内的指令或 Shell Script.