萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 服務開機自啟動完整管理指南:systemd、chkconfig、rc.local 三大機制深度解析

Linux 服務開機自啟動全機制解析:從 System V 到 systemd 的演進與最佳實踐

Linux 服務開機自啟動機制經歷了 System V init(chkconfig)、rc.local 到 systemd 的演進。理解三種機制的差異、適用場景與操作指令,是系統管理員核心基本功。本文提供完整操作指南與避坑指引。

一、systemd:現代標準(CentOS 7+、RHEL 7+、Ubuntu 16.04+、Debian 8+)

核心指令對照表

操作systemd 指令舊 SysV 指令
啟動服務systemctl start nginxservice nginx start
停止服務systemctl stop nginxservice nginx stop
重啟服務systemctl restart nginxservice nginx restart
重新載入配置systemctl reload nginxservice nginx reload
查看狀態systemctl status nginxservice nginx status
設定開機自啟systemctl enable nginxchkconfig nginx on
取消開機自啟systemctl disable nginxchkconfig nginx off
檢查是否開機自啟systemctl is-enabled nginxchkconfig --list nginx
列出所有單元systemctl list-unit-files --type=servicechkconfig --list
查看失敗單元systemctl --failed無對應

systemd 服務單元檔案結構


# /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/)。

自訂服務建立流程

  1. 編寫啟動腳本 /usr/local/bin/myapp-start.sh(確保可執行 chmod +x)
  2. 建立單元檔 /etc/systemd/system/myapp.service
  3. systemctl daemon-reload 重新載入
  4. systemctl enable --now myapp 啟用並立即啟動

二、chkconfig:System V 相容層(CentOS 6、舊版相容)

適用場景

  • CentOS 6 / RHEL 6 及更舊版本
  • 第三方軟體僅提供 SysV 風格啟動腳本(/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

SysV 腳本標準註解(支援 chkconfig 必備)


#!/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

三、rc.local:傳統簡易啟動(不推薦生產環境)

適用場景

  • 簡單指令、腳本需開機執行
  • 不需依賴管理、狀態監控、依賴排序
  • 測試環境、臨時方案

啟用流程


# 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/openSUSE 特殊機制


# 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用途
0haltpoweroff.target關機
1singlerescue.target單用戶維護模式
2multiusermulti-user.target多用戶無圖形(無 NFS)
3multiuser+NFSmulti-user.target多用戶命令列(標準伺服器)
4unused-保留/自定義
5graphicalgraphical.target多用戶圖形介面
6rebootreboot.target重啟

六、最佳實踐建議

  1. 優先使用 systemd 原生單元:功能最完整、依賴解析最精確、日誌整合最佳
  2. 避免混用機制:同一服務勿同時用 systemd 與 rc.local 啟動
  3. 第三方軟體遷移:將舊 /etc/init.d/ 腳本轉換為 systemd 單元
  4. 模板化管理:用 Ansible/Jinja2 模板批次生成單元檔,確保一致性
  5. 健康檢查整合:單元檔加入 ExecStartPre 預檢、WatchdogSec 看門狗

結語

systemd 已成事實標準,掌握單元檔撰寫、依賴聲明、目標單元關係是現代 Linux 運維核心技能。將 chkconfig/rc.local 視為歷史兼容層,新專案全面擁抱 systemd。

copyright © 萬盛學電腦網 all rights reserved