在 Linux (System V init) 系統中,編寫符合規範的 init 腳本並註冊到 chkconfig,即可實現服務的開機自啟、手動控制與運行級別管理。
#!/bin/sh
# chkconfig: 2345 80 05
# description: 自定義服務描述
case "$1" in
start)
# 啟動命令,如:/usr/local/myapp/bin/start.sh
;;
stop)
# 停止命令,如:/usr/local/myapp/bin/stop.sh
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
# chkconfig: 2345 80 05# description: 服務描述,不可省略,供 chkconfig --list 顯示myservice,複製至 /etc/init.d/chmod +x /etc/init.d/myservicechkconfig --add myservice 註冊chkconfig --list 驗證輸出中出現 myservice 0:off 1:off 2:on 3:on 4:on 5:on 6:off此後可用 service myservice start|stop|restart 管理,開機自動按運行級別啟動/關閉。