rc是runlevel control directory的縮寫。
大多數的Linux 發行版本中,啟動指令碼都被放在/etc/rc.d/init.d。這些指令碼被ln 命令來串連到 /etc/rc.d/rcn.d 目錄。(這裡的n 就是運行級0-6)
init.d/ :各種伺服器和程式的二進位檔案存放目錄。
rcx.d/: 各個啟動層級的執行程式串連目錄。裡頭的東西都是指向init.d/的一些軟串連。
還有三個指令碼:rc.sysinit, rc, rc.local
redhat的啟動方式和執行次序是:
載入核心 執行init程式 /etc/rc.d/rc.sysinit # 由init執行的第一個指令碼
/etc/rc.d/rc $RUNLEVEL # $RUNLEVEL為預設的運行模式
/etc/rc.d/rc.local /sbin/mingetty # 等待使用者登入
/etc/rc.d/rc.sysinit主要做在各個運行模式中相同的初始化工作,包括: 調入keymap以及系統字型 啟動swapping 設定主機名稱 設定NIS網域名稱 檢查(fsck)並mount檔案系統 開啟quota 裝載音效卡模組 設定系統時鐘 等等。
/etc/rc.d/rc則根據其參數指定的運行模式(運行層級,你在inittab檔案中可以設定)來執行相應目錄下的指令碼。
凡是以Kxx開頭的 ,都以stop為參數來調用;凡是以Sxx開頭的,都以start為參數來調用。調用的順序按xx 從小到大來執行。
例如,假設預設的運行模式是3,/etc/rc.d/rc就會按上述方式調用 /etc/rc.d/rc3.d/下的指令碼。
值得一提的是,Redhat中的運行模式2、3、5都把/etc/rc.d/rc.local做為初始化指令碼中 的最後一個,所以使用者可以自己在這個檔案中添加一些需要在其他初始化工作之後,登入之前執行的命令。 init在等待/etc/rc.d/rc執行完畢之後(因為在/etc/inittab中/etc/rc.d/rc的 action是wait),將在指定的各個虛擬終端上運行/sbin/mingetty,等待使用者的登入。
Fedora 設定開機啟動指令碼
sudo touch /etc/rc.d/rc.local
sudo vim /etc/rc.d/rc.local
在/etc/rc.d/rc.local檔案中寫入, 然後使用:wq命令 儲存並退出.
#!/bin/bash
# 在這個檔案中寫入開機啟動需要執行的命令
賦予可執行許可權:
sudo chmod+x /etc/rc.d/rc.local
設定開機啟動:
sudo systemctl enable rc-local.service
如果出現以下錯誤提示:
[root@abc ~]# systemctl enable rc-local.service
The unit files have no [Install] section. 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, ...).
sudo vim /usr/lib/systemd/system/rc-local.service 在rc-local.service檔案末尾加入:
[Install]
WantedBy=multi-user.target
並重新設定開機啟動:
sudo systemctl enable rc-local.service
查看狀態, 如果出現以下提示, 設定成功!