Procd init script duplicates itself when restarted

Hello. I want my script to start with system startup and restart in case it breaks. I'm using the official openwrt documentation and have no idea why the script restarts when I use /etc/init.d/myscript stop and not only restarts, but creates two instances of my script.
Without procd_set_param respawn it works as expected. I understand that on stop or restart this restart parameter recognises that the script has broken down and restarts it, but I don't know how to fix it. Do you have any idea? Thanks in advance

#!/bin/sh /etc/rc.common

START=99
STOP=1
USE_PROCD=1
PROCD_DEBUG=1

SCRIPT="/root/carambola2/source/myscript.sh"
PIDFILE=/tmp/myscript.pid

start_service() {
        procd_open_instance
        procd_set_param pidfile $PIDFILE
        procd_set_param stdout 1
        procd_set_param stderr 1
        procd_set_param command sh "$SCRIPT"
        procd_set_param respawn 
        procd_close_instance
}

stop_service() {
        kill $(cat $PIDFILE)
}

stop_service() is called after procd already killed the service, usually stop_service() is only needed when you need additional things to do after your service has been killed by procd. The service itself should run in the foreground. The procd command should directly call your script, remove the needless "sh". For working procd examples scan the OpenWrt package repo (https://github.com/openwrt/packages).

1 Like

Thanks for the response.
After stopping the service (without stop_service() function) there is no service process running anymore, but myscript.sh still works, it is not killed or restarted. That's why I userd kill $(cat $PIDFILE) previously.
Do you know why this happens?

Please start your service and provide the output of:

ubus call service list '{"name":"xxxSERVICENAMExxx"}'

{
	"myservice": {
		"instances": {
			"instance1": {
				"running": true,
				"pid": 1146,
				"command": [
					"\/root\/carambola2\/source\/autostart.sh"
				],
				"term_timeout": 5,
				"respawn": {
					"threshold": 3600,
					"timeout": 5,
					"retry": 5
				}
			}
		}
	}
}

Does what you're running do it's own daemonization? That tends to confuse procd. If you can run it foreground that would help you a great deal.

Hello. In the autostart.sh file there is a python script running. If I call /etc/init.d/myscript stop, the autostart.sh stops, but a python script is still running. If I add killing python scipt to stop_service(), it is being respawned.