# chkconfig --add myservice
service myservice does not support chkconfig
chkconfig 掃描 /etc/init.d/ 下腳本前幾行尋找特定註解標記,缺少或格式錯誤即報錯。
#!/bin/bash
# chkconfig: 2345 80 90
# description: 服務描述文字
#!/bin/bash:指定解譯器,所有 Shell 腳本必備# chkconfig: 2345 80 90:三參數含義:
S##)效果:rc2.d~rc5.d 建立 S80myservice;rc0.d/rc6.d 建立 K90myservice
# 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
# 建立 /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