Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

解決「service 不支援 chkconfig」錯誤:標準化啟動腳本頭部註解規範

編輯:Linux 教學

錯誤現象

# chkconfig --add myservice
service myservice does not support chkconfig

根本原因

chkconfig 掃描 /etc/init.d/ 下腳本前幾行尋找特定註解標記,缺少或格式錯誤即報錯。

標準腳本頭部格式 (前三行強制)

#!/bin/bash
# chkconfig: 2345 80 90
# description: 服務描述文字

逐行解析

  1. #!/bin/bash:指定解譯器,所有 Shell 腳本必備
  2. # chkconfig: 2345 80 90:三參數含義:
    • 2345:於 runlevel 2、3、4、5 建立啟動連結 (S##)
    • 80:啟動序號 (S80),數字越小越早啟動
    • 90:停止序號 (K90),數字越小越早停止

    效果:rc2.d~rc5.d 建立 S80myservice;rc0.d/rc6.d 建立 K90myservice

  3. # description: ...:服務說明,chkconfig --list 顯示用

常見錯誤與修正

錯誤寫法問題修正
缺少第 2、3 行chkconfig 無法解析補全兩行註解
#chkconfig:2345 80 90冒號後無空格加空格 # chkconfig: 2345 80 90
# chkconfig: 345 80 90缺少 runlevel 2視需求補全
# description:service無冒號空格# description: Service Name
腳本無執行權限chkconfig 不報錯但無法啟動chmod +x /etc/init.d/myservice

快速修復腳本

#!/bin/bash
# fix_chkconfig.sh - 批次修正腳本頭部
SCRIPT_DIR="/etc/init.d"
for f in "$SCRIPT_DIR"/*; do
    [[ -f "$f" && -x "$f" ]] || continue
    head -3 "$f" | grep -q "chkconfig:" && continue
    echo "Fixing $f"
    sed -i '1a# chkconfig: 2345 80 90\n# description: Auto-fixed service' "$f"
done

替代方案:手動建立符號連結

若腳本極其簡單或不想修改,可手動建立連結達到相同效果:

# 啟動連結 (runlevel 3,5)
ln -s /etc/init.d/myservice /etc/rc3.d/S80myservice
ln -s /etc/init.d/myservice /etc/rc5.d/S80myservice

# 停止連結 (runlevel 0,6)
ln -s /etc/init.d/myservice /etc/rc0.d/K90myservice
ln -s /etc/init.d/myservice /etc/rc6.d/K90myservice

systemd 時代的對應 (RHEL 7+)

# 建立 /etc/systemd/system/myservice.service
[Unit]
Description=My Service
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/myservice start
ExecStop=/etc/init.d/myservice stop
PIDFile=/var/run/myservice.pid

[Install]
WantedBy=multi-user.target

# 啟用
systemctl daemon-reload
systemctl enable myservice
systemctl start myservice
Copyright © Windows教程網 All Rights Reserved