[SOLVED] Simple scholar procd init script doesn't respond

Hi,

I'm running OpenWrt 18.06

I have three scripts.
The main script is "tata.sh" and I have two other scripts that run / kill this script : "tata_start.sh" / "tata_end.sh"

The first script (tata.sh) runs an infiinte loop giving me its PID and PPID:

#!/bin/ash
while :
do
        echo "tata : Pid=$$ $(grep PPid /proc/$$/status)"
        sleep 10
done

tata_start.sh runs tata.sh in background

#!/bin/ash
echo "$0 : Pid=$$ $(grep PPid /proc/$$/status)"
./tata.sh &

tata_end.sh kills any tata.sh script running in background

#!/bin/ash
echo "$0 : Pid=$$ $(grep PPid /proc/$$/status)"
pgrep -f tata.sh && pkill -f tata.sh

If I execute both of them at the prompt, it works well (tata_start.sh runs start.sh in background and tata_end.sh kills the background tata.sh process)

$> ./tata_start.sh && ./tata_end.sh 
./tata_start.sh : Pid=6041 PPid:        413
./tata.sh : Pid=6043 PPid:      1
./tata_end.sh : Pid=6044 PPid:  413
6043

If I run both in a procd init script, I can start the service but then it is stuck when I try to stop it.

#!/bin/sh /etc/rc.common
USE_PROCD=1

start_service(){
        /root/tata_start.sh
}

stop_service(){
        /root/tata_end.sh
}

I know that there is a procd_instance that I should be using but I just don't understand why the service gets stuck at "flock -n 1000" and doesn't just kill tata.sh and exit.

Thanks

This is due to way how you fork the background process. It inherits the lock fd 1000 and keeps it busy, causing subsequent init actions to fail.

In general, the processes initiated by procd init scripts must stay in foreground and may not daemonize themselves. Doing so breaks the process supervision, and in the case of unwillingly inherited descriptors, may cause blocking resources (open files, sockets, ...)

2 Likes

Thanks Jow for your answer,

True, I completely forgot that scripts in a service must run in foreground.
I don't understand the exact mechanism that creates that deadlock but at least I know that the behavior is "normal".

So each time you need to execute a script that must continue for some time in the background you HAVE TO create a service?

It would look like this:

start_service()
{
     procd_open_instance ...
     ...
     procd_close_instance
}
stop_service()
{
      # the only solution for having a script running in background when THIS service stops is to run ANOTHER service ?
      /etc/init.d/ANOTHER_service_for_script_in_background_after_THIS_service_stops start
}

Thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.