Procd process management service of openwrt

RPC daemon—rpcd, OpenWrt process management service

It is not efficient to write a daemon for each piece of software and run them independently. This is why rpcd was developed. This is a small daemon that supports plug-ins using common APIs, dynamically registers, and adds your own applications to the rpcd daemon management service.

The following example realizes the function:

  • Add the program /usr/bin/main to procd for management, and it will start automatically after power on
  • When main exits abnormally, it restarts automatically.
  • At the same time, it monitors whether the configuration file /etc/config/mqtt has changed. If there is a change, the process is automatically restarted.

Add the following script to /etc/init.d:

#!/bin/sh /etc/rc.common
# /etc/init.d
# Copyright (C) 2008 OpenWrt.org   

# 执行的顺序,在/etc/rc.d目录下自动生成S98xxx,K98xxxx,按照顺序执行。
START=98
STOP=98

# 使用procd启动
USE_PROCD=1

DAEMON=main
PROG=/usr/bin/$DAEMON

CONFIG=mqtt
CONFIGFILE=/etc/config/$CONFIG


# 向 procd 注册并启动服务,是将在 services 所管理对象里面增加了一项
start_service() {
    
    
	echo "start user service!"

	# 开始增加一个服务实例, 在procd看来一个应用程序可以多个实例
	# ubus call service list 可以查看实例
	procd_open_instance
	
	#定义respawn参数,进程意外退出的重启机制及策略
	# threshold:异常失败边界值3600;timeout:重启延迟时间5;retry:失败重启次数5
	# procd_set_param respawn retry=10
	procd_set_param respawn 3600 5 10
	
	# 执行的命令是"/usr/bin/main", 若后面有参数可以直接在后面加上
	procd_set_param command $PROG '&'

	# 配置文件名,比较其文件内容是否改变
	# procd_set_param file $CONFIGFILE

	# 绑定的网络设备(探测 ifindex 更改)
	# netdevs=
	# procd_set_param netdev $netdevs

	# 指定对应的pidfile
	# procd_set_param pidfile /var/run/${DAEMON}.pid
	

	# 完成进程实例的增加
	procd_close_instance
}

# 让 procd 解除注册,并关闭服务, 是将在 services 中的管理对象删除
stop_service() {
    
    
	echo "stop user service!"
	# rm -f /var/run/${DAEMON}.pid
	# service_stop "$PROG"
	killall $DAEMON
}

# 重启服务,如果定义了该函数,在 reload 时将调用该函数,否则再次调用 start 函数
reload_service(){
    
    
	echo "reload user service!"
	stop
	start
}

# 配置文件或网络接口改变之后触发服务重新读取配置
service_triggers(){
    
    
	echo "triggers user service!"
	procd_add_reload_trigger $CONFIG
}

# 用于判断进程是否启动成功
# service_started(){
    
    

# }


restart() {
    
    
   stop
   start
}

Guess you like

Origin blog.csdn.net/pyt1234567890/article/details/111370310