在 CentOS 6 / RHEL 6 等使用 SysV init 的系統上,將自訂腳本加入開機自啟動的標準流程:編寫符合規範的 init 腳本 → 放入 /etc/init.d/ → chkconfig --add 註冊。
# vim /etc/init.d/auto_coreseek.sh
內容如下:
#!/bin/sh # chkconfig: 2345 80 90 # description: auto start coreseek searchd /usr/local/coreseek/bin/searchd
關鍵標註說明:
-# chkconfig: 2345 80 90
2345:在執行等級 2、3、4、5 啟動(多用戶/圖形模式)。
80:啟動優先序(數字越小越早)。
90:關機停止優先序(數字越小越早停止)。
-# description:服務描述,供chkconfig --list顯示。
chmod +x /etc/init.d/auto_coreseek.sh chkconfig --add auto_coreseek.sh
chkconfig --list auto_coreseek.sh # 輸出應包含 2:on 3:on 4:on 5:on # 手動測試 service auto_coreseek.sh start service auto_coreseek.sh status
| 等級 | 名稱 | 說明 |
|---|---|---|
| 0 | 關機 | 系統停止 |
| 1 | 單用戶 | 維護模式 |
| 2 | 多用戶無網路 | 命令列模式 |
| 3 | 多用戶有網路 | 標準伺服器模式 |
| 4 | 保留 | 通常未使用 |
| 5 | 圖形介面 | 桌面環境 |
| 6 | 重啟 | 重新開機 |
新版系統請改寫為 systemd unit 檔:
# /etc/systemd/system/coreseek.service [Unit] Description=Coreseek Search Daemon After=network.target [Service] Type=forking ExecStart=/usr/local/coreseek/bin/searchd ExecStop=/usr/local/coreseek/bin/searchd --stop User=coreseek [Install] WantedBy=multi-user.target
啟用:
systemctl daemon-reload systemctl enable coreseek systemctl start coreseek
掌握 SysV init 腳本規範與 chkconfig 操作,是維護舊版 Linux 系統的基本功;新系統請直接採用 systemd,更強大且標準化。