參考 https://wiki.openwrt.org/doc/techref/initscripts
以一個簡單的例子來說明
#!/bin/sh /etc/rc.common # Example script # Copyright (C) 2007 OpenWrt.org START=10 STOP=15 start() { echo start # commands to launch application } stop() { echo stop # commands to kill application }
第一行shebang #! 使用 /bin/sh /etc/rc.common 作為腳本解釋器並在執行腳本前調用 main 和檢查腳本
公用的 init script 方法有
start # 啟動服務
stop # 停止服務
restart # 重啟服務
reload # 重新載入配置文件, 如果失敗則重啟
enable # 啟用開機自啟動
disable # 禁用開機自啟動
腳本中 start() 和 stop() 是必須的
啟動順序
START= 和 STOP= 決定腳本啟動時的次序. 啟動時init.d會根據文件名順序, 自動執行在/etc/rc.d中找到的腳本. 初始化腳本可以作為/etc/init.d/下文件的軟鏈放置在/etc/rc.d/. enable 和 disable 可以自動幫你創建對應的帶序號的軟鏈.
這個例子中START=10 會被鏈接到 /etc/rc.d/S10example, 啟動時執行在START=9之后, 在START=11之前. 而STOP=15會被鏈接到 /etc/rc.d/K15example, 執行在STOP=14之后, 在STOP=16之前. 同一個啟動數字的, 按字母順序啟動.
腳本中的 boot()
當存在boot()方法時, 系統啟動時會調用boot()而不是start()
boot() { echo boot # commands to run on boot }
你可以使用EXTRA_COMMANDS和EXTRA_HELP設置自定義的服務方法
EXTRA_COMMANDS="custom" EXTRA_HELP=" custom Help for the custom command" custom() { echo "custom command" # do your custom stuff }
多個自定義方法的添加
EXTRA_COMMANDS="custom1 custom2 custom3" EXTRA_HELP=<<EOF custom1 Help for the custom1 command custom2 Help for the custom2 command custom3 Help for the custom3 command EOF custom1 () { echo "custom1" # do the stuff for custom1 } custom2 () { echo "custom2" # do the stuff for custom2 } custom3 () { echo "custom3" # do the stuff for custom3 }
快速查詢所有服務的自啟動狀態, 可以使用以下命令
root@OpenWrt:~# for F in /etc/init.d/* ; do $F enabled && echo $F on || echo $F **disabled**; done /etc/init.d/boot on /etc/init.d/bootcount on /etc/init.d/cron on /etc/init.d/dnsmasq on /etc/init.d/done on /etc/init.d/dropbear on /etc/init.d/firewall on /etc/init.d/fstab on /etc/init.d/gpio_switch on /etc/init.d/led on /etc/init.d/log on /etc/init.d/network on /etc/init.d/odhcpd on /etc/init.d/rpcd on /etc/init.d/samba on /etc/init.d/shadowsocks-libev on /etc/init.d/sysctl on /etc/init.d/sysfixtime on /etc/init.d/sysntpd on /etc/init.d/system on /etc/init.d/transmission on /etc/init.d/uhttpd on /etc/init.d/umount **disabled** /etc/init.d/wifidog **disabled**