怎样写一个基于procd的init脚本?
https://wiki.openwrt.org/zh-cn/inbox/procd-init-scripts English
Table of Contents
这个页面还在不断完善中 …
怎样写一个基于procd的init脚本?
procd init 脚本跟原来的init脚本有些类似,但是最主要的不同是,procd期望服务 运行在前台
它作为已存在的脚本必须以sh /etc/rc.common
开始, 为兼容考虑也需要 "USE_PROCD=1" 行.
#!/bin/sh /etc/rc.common USE_PROCD=1
启动一个服务我们需要一个函数'start_service',stop_service函数可选,只在你需要在停止服务的时候需要做某些事的时候需要。 stop_service() 在procd杀死这个服务后调用。 The service itself should run in the foreground. (Apparently
start_service() { procd_open_instance procd_set_param command /sbin/your_service_daemon -b -a --foo procd_set_param respawn # respawn automatically if something died, be careful if you have an alternative process supervisor procd_set_param env SOME_VARIABLE=funtimes # pass environment variables to your process procd_set_param limits core="unlimited" # If you need to set ulimit for your process procd_set_param file /var/etc/your_service.conf # /etc/init.d/your_service reload will restart the daemon if these files have changed procd_set_param netdev dev # likewise, except if dev's ifindex changes. procd_set_param data name=value ... # likewise, except if this data changes. procd_close_instance }
TODO: Table old openwrt initscript ↔ new procd
For as much information as is available, see the documentation at the top of procd.sh
Procd triggers on config file / network interface changes
In older versions of OpenWrt, a system called "ucitrack" attempted to track UCI config files, and the processes that depended on each of them, and would restart them all as needed. This too, is replaced with ubus/procd, and expanded to allow notifying services when network interfaces change. This is useful for services like dnsmasq, and proxy/routing software that cares about which network interfaces are in use, and with what configuration.
First, to simply make your service depend on a config file, add a "service_triggers()" clause to your init script
service_triggers() { procd_add_reload_trigger "uci-file-name" }
This will setup hooks such that issuing 'reload_config
' will issue a call to '/etc/init.d/<yourinitscript> reload
' when the md5sums of '/etc/config/uci-file-name
' has changed. You can edit as many config files as you like, and then issue reload_config, procd will take care of reloading all of them. Note, no change in the config file, no reload. If you want to explicitly reload, you still need to issue '/etc/init.d/<yourservice> reload
' manually.
By default, "reload" will cause a stop/start call, unless you have provided the 'reload()
' call explicitly in your init script. There is not (currently, r41147) support for grabbing the PID of a procd service, or sending a signal to that service in the reload routine, but it should be possible "soon™"
reload() { service_reload printf "service reloaded at %s" "$(date)" >> /tmp/somefile }
If you want/need your service to depend on changes to networking, simply modify your service_triggers section, like so..
service_triggers() { procd_add_reload_trigger "uci-file-name" "second-uci-file" procd_add_network_trigger "lan"|"etho0" FIXME - this is still a work in process.... }
igmpproxy is (currently) the only service that makes use of this, but (hopefully) by the time you read this, dnsmasq will also have been updated, at least.
How do these scripts work?
All arguments are packed into json and send over to procd via ubus