Linux 服務開機自啟動機制經歷了 System V init(chkconfig)、rc.local 到 systemd 的演進。理解三種機制的差異、適用場景與操作指令,是系統管理員核心基本功。本文提供完整操作指南與避坑指引。
| 操作 | systemd 指令 | 舊 SysV 指令 |
|---|---|---|
| 啟動服務 | systemctl start nginx | service nginx start |
| 停止服務 | systemctl stop nginx | service nginx stop |
| 重啟服務 | systemctl restart nginx | service nginx restart |
| 重新載入配置 | systemctl reload nginx | service nginx reload |
| 查看狀態 | systemctl status nginx | service nginx status |
| 設定開機自啟 | systemctl enable nginx | chkconfig nginx on |
| 取消開機自啟 | systemctl disable nginx | chkconfig nginx off |
| 檢查是否開機自啟 | systemctl is-enabled nginx | chkconfig --list nginx |
| 列出所有單元 | systemctl list-unit-files --type=service | chkconfig --list |
| 查看失敗單元 | systemctl --failed | 無對應 |
# /etc/systemd/system/nginx.service(或 /usr/lib/systemd/system/)
[Unit]
Description=NGINX Web Server
After=network.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
關鍵段落: [Install] 區段的 WantedBy=multi-user.target 決定 systemctl enable 時建立的軟連結位置(/etc/systemd/system/multi-user.target.wants/)。
/usr/local/bin/myapp-start.sh(確保可執行 chmod +x)/etc/systemd/system/myapp.servicesystemctl daemon-reload 重新載入systemctl enable --now myapp 啟用並立即啟動/etc/init.d/)
# 查看所有服務各運行級別狀態
chkconfig --list
# 添加服務(腳本需放在 /etc/init.d/ 並含 chkconfig 註解)
chkconfig --add myservice
# 設定運行級別 2345 啟動,啟動順序 80,停止順序 20
chkconfig --level 2345 myservice on
chkconfig myservice 80 20
# 刪除服務
chkconfig --del myservice
#!/bin/bash
# /etc/init.d/myservice
#
# chkconfig: 2345 80 20
# description: My Custom Service
# processname: myservice
# pidfile: /var/run/myservice.pid
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: My Custom Service
### END INIT INFO
# 1. 確保檔案存在且可執行
touch /etc/rc.local
chmod +x /etc/rc.local
# 2. 編輯內容(必須以 #!/bin/bash 開頭)
cat > /etc/rc.local << 'EOF'
#!/bin/bash
# 自定義開機啟動命令
/usr/local/nginx/sbin/nginx
/usr/local/php/sbin/php-fpm
/usr/local/mysql/bin/mysqld_safe --user=mysql &
# 自定義腳本
/opt/scripts/start_monitoring.sh
EOF
# 3. systemd 管理 rc-local 服務(現代系統)
systemctl enable rc-local
systemctl start rc-local
# SUSE 使用 before.local / after.local
# /etc/init.d/before.local -> 切換運行級別前執行
# /etc/init.d/after.local -> 切換運行級別後執行
# 建立並啟用
touch /etc/init.d/after.local
chmod +x /etc/init.d/after.local
echo "/usr/local/apache/bin/apachectl start" >> /etc/init.d/after.local
| 級別 | System V 名稱 | systemd Target | 用途 |
|---|---|---|---|
| 0 | halt | poweroff.target | 關機 |
| 1 | single | rescue.target | 單用戶維護模式 |
| 2 | multiuser | multi-user.target | 多用戶無圖形(無 NFS) |
| 3 | multiuser+NFS | multi-user.target | 多用戶命令列(標準伺服器) |
| 4 | unused | - | 保留/自定義 |
| 5 | graphical | graphical.target | 多用戶圖形介面 |
| 6 | reboot | reboot.target | 重啟 |
/etc/init.d/ 腳本轉換為 systemd 單元ExecStartPre 預檢、WatchdogSec 看門狗systemd 已成事實標準,掌握單元檔撰寫、依賴聲明、目標單元關係是現代 Linux 運維核心技能。將 chkconfig/rc.local 視為歷史兼容層,新專案全面擁抱 systemd。