OpenWrt使用procd作为守护进程管理,通过下面代码可以手动添加守护进程及设置开机启动

一些简单的命令可以写入/etc/rc.local简单地实现开机启动,守护进程适用于需要自动重启崩溃的应用的情况

  1. 首先安装好需要添加到守护进程的应用,并确定可执行文件的绝对路径 这里使用samplebin作为例子,安装的绝对路径为/usr/bin/samplebin

  2. 将下面的配置保存到/etc/init.d文件夹下面
    samplebin
    #!/bin/sh /etc/rc.common
    # put this file in /etc/init.d/
    USE_PROCD=1
    START=95
    STOP=01
    start_service() {
     procd_open_instance samplebin
     procd_set_param command /usr/bin/samplebin # command for running app
     procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-0} # restart app for unlimited times
     procd_set_param stdout 1 # forward stdout of the command to logd
     procd_set_param stderr 1 # same for stderr
     procd_close_instance
    }
    

    上面这段配置会在开机顺序为95(越大越晚启动)时启动samplebin
    如果应用崩溃,守护进程会无限次尝试重启应用

  3. 给文件添加可执行权限
    chmod a+x /etc/init.d/samplebin
    
  4. 设置为开机启动
    service samplebin enable
    
  5. 立即运行应用
    service samplebin start
    

参考: Openwrt developer guide

分类:

更新时间:

留下评论