OpenWrt Forum Archive

Topic: Auto run any terminated process

The content of this topic has been archived on 6 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi All,

I have written a program of TCP server. Using Init Scripts I am starting my program at boot up.
If some how (e.g. segmentation fault) my program get terminated, What is the way to start this program again automatically?


Thanks in advance.

To solve this I have wrote a scripts calls monitor.sh

    #!/bin/sh
    
    while [ 1 ]; do
    
        PROCESS=`ps | grep myApp | grep -v grep`
    
        if [ -z "$PROCESS" ]; then
             echo "Process is not running" 
            /root/myApp  2>/dev/null 1>/dev/null &
                       
        fi
        
        sleep 3
    done
    exit 0

Please comment on this.

I'm not sure which revision of OpenWRT you're using, so I don't know if my comment is applicable, but at least my Chaos Calmer build comes with a process management daemon called procd, so perhaps you could write your init-script in a compatible format, and let procd deal with the monitoring aspects?

See https://wiki.openwrt.org/inbox/procd-init-scripts for some information, and review the existing scripts in /etc/rc.d/ for some examples. There's also the procd reference available at http://wiki.prplfoundation.org/wiki/Procd_reference

These documents are far from complete or concise, but hopefully they give you some ideas. Perhaps the 'procd_set_param respawn' is what you're after?

The discussion might have continued from here.