Procd init involving python programs duplicates if restarted

This topic is a complement to Procd init script duplicates itself when restarted

Context:

  • I use a Glinet X750 running OpenWrt 19.07.8, r11364-ef56c85848
  • I have an Initd Script calling a busybox script which prepares the hardware then runs a program in python.

As lexanderpl points, when stopping or restarting the Initd service, it fails to fully stop and the python continues to run in background with any log to notice such behavior. The only way to know if the python program is still running is using the ps:

I wrote a test code "loop" to reproduce the issue following the same steps

  • An Initd script loop-launch running loop.sh at boot
  • The script loop.sh runs loop.py which runs a ping to 8.8.8.8 once

Here is the content of the Initd script loop-launch

#!/bin/sh /etc/rc.common
USE_PROCD=1
START=99
STOP=01
start_service() {
    procd_open_instance
    procd_set_param command /bin/sh" /test/loop.sh"
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_set_param respawn ${respawn_threshold:-60} ${respawn_timeout:-5} ${respawn_retry:-99}
    procd_close_instance
}

Here is the content of the Initd script loop.sh

#!/bin/sh

while true
do
      python /test/loop.py
      sleep 10
done

Here is the content of the Initd script loop.py

#!/bin/sh

while true
import logging
import time
import sys
import subprocess


dbgfmt = logging.Formatter("%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s")

hdbg = logging.StreamHandler(sys.stdout)
hdbg.setLevel(logging.DEBUG)
hdbg.setFormatter(dbgfmt)

logger = logging.getLogger("Loop app")
logger.setLevel(logging.DEBUG)
logger.addHandler(hdbg)


n=0

while(1):
  ping_process = subprocess.Popen("8.8.8.8 -c 1",
                      bufsize=1, universal_newlines=True,
                      stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                      shell=True)
  logger.info("Loop is still running - {}".format(n))
  n+=1
  time.sleep(2)

Once enabling the service, I start and stop the service multiple times then I can see in the logs that the service and the python program stops every time but it actually keeps running in the background

Mon May 29 13:20:23 2023 daemon.info sh[7698]: 2023-05-29 13:20:23,656 -- Loop app -- INFO -- Loop is still running - 629
Mon May 29 13:20:25 2023 daemon.info sh[7698]: 2023-05-29 13:20:25,704 -- Loop app -- INFO -- Loop is still running - 630
Mon May 29 13:20:27 2023 daemon.info sh[7698]: 2023-05-29 13:20:27,743 -- Loop app -- INFO -- Loop is still running - 631
Mon May 29 13:20:29 2023 daemon.info sh[7698]: 2023-05-29 13:20:29,762 -- Loop app -- INFO -- Loop is still running - 632
Mon May 29 13:20:31 2023 daemon.info sh[7698]: 2023-05-29 13:20:31,783 -- Loop app -- INFO -- Loop is still running - 633
Mon May 29 13:21:18 2023 daemon.info sh[7555]: 2023-05-29 13:21:18,875 -- Loop app -- INFO -- Loop is still running - 0
Mon May 29 13:21:20 2023 daemon.info sh[7555]: 2023-05-29 13:21:20,899 -- Loop app -- INFO -- Loop is still running - 1
Mon May 29 13:21:22 2023 daemon.info sh[7555]: 2023-05-29 13:21:22,929 -- Loop app -- INFO -- Loop is still running - 2
Mon May 29 13:21:24 2023 daemon.info sh[7555]: 2023-05-29 13:21:24,954 -- Loop app -- INFO -- Loop is still running - 3
...
Mon May 29 13:22:09 2023 daemon.info sh[7555]: 2023-05-29 13:22:09,641 -- Loop app -- INFO -- Loop is still running - 25
Mon May 29 13:22:11 2023 daemon.info sh[7555]: 2023-05-29 13:22:11,673 -- Loop app -- INFO -- Loop is still running - 26
Mon May 29 13:22:11 2023 daemon.err sh[7555]: Terminated
Mon May 29 13:22:40 2023 daemon.info sh[9541]: 2023-05-29 13:22:40,354 -- Loop app -- INFO -- Loop is still running - 0
Mon May 29 13:22:42 2023 daemon.info sh[9541]: 2023-05-29 13:22:42,376 -- Loop app -- INFO -- Loop is still running - 1
Mon May 29 13:22:44 2023 daemon.info sh[9541]: 2023-05-29 13:22:44,423 -- Loop app -- INFO -- Loop is still running - 2

However loop.py keeps running in background and I confirmed it by running a tcpdump in the device

user@GL-X750:~# ps | grep python
15311 root      6716 S    python3 /test/loop.py
18688 root      6716 S    python3 /test/loop.py
20794 root      6716 S    python3 /test/loop.py
20810 root      6716 S    python3 /test/loop.py
19336 root      1224 S    grep python

I don't fully understand why the python script is not stopped and why it is launched every time, this is not an issue with classic Systemd services, but based on the docs I realised I need to kill the python process when stopping the service so I added a stop_service

stop_service() {
        echo -e "Stopping loop"
        # commands to kill application
        PTYHON_PROCESSES=$(ps | grep python | grep loop | awk '{print $1}' | tr "\n" " ")
        kill PTYHON_PROCESSES
}

Although this approach works, I think I'm missing something so if someone else has or had already this problem here's a solution and I would be glad to read other approaches