在 Redhat / Centos 開機後, 如果想自動執行一些 shell script 或指令, 可以直接編輯 /etc/rc.local 檔案.
注: RHEL 及 CentOS 7 預設不會啟動 /etc/rc.local, 需要先執行以下指令才可以:
# chmod +x /etc/rc.local
以下是 /etc/rc.local 預設內容:
|
1 2 3 4 5 6 7 |
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local |
要加入自動執行指令或 shell script 十分簡單, 只要直接加上要執行的指令即可, 例如我設定了每次開機會自動恢復 iptables 的設定, 便加入了 iptables-restore 指令:
|
1 2 3 4 5 6 7 8 |
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /sbin/iptables-restore < /root/iptables-save |
儲存檔案後就完成了, 下次開機便會自動執行加入的指令.