0

I have developed a daemon in Linux. It works correctly, however, when I set that script to be started at boot time, it has conflict with other service, so I need to start it after all services have started.

How can I add a delay in the init.d shell script or inside the service itself without affecting booting the system?

This is Debian Linux.

Regards Jaime

jstuardo
  • 155
  • 1
  • 7
  • First thing to do is understand _why_ it has conflict with another service. Once this step is achieved, you can think about a solution. And if you want to be helped, please share evidences (command, logs, error msg, ...) of your assumptions. You can even share your debian version, just to know if you are supposed to use systemd instead of legacy /etc/init.d subsystem. – Chaoxiang N Dec 04 '19 at 19:32
  • I know why it has the conflict. This is not a computer but an RFID device. My daemon uses a hardware module that is initialized by other daemon. If both daemons are initialized at the same time, that hardware module get blocked. In short, my daemon should be started after that hardware module is completely initialized, that is, after the daemon that initializes that module has done it. This is the OS version: Debian GNU/Linux 7.8 (2015-01-27) – jstuardo Dec 04 '19 at 19:55

1 Answers1

0

one possible option is to include such a piece of code in your init.d script.

of course you need to change the test part lsmod | grep -qw module_name to verify that the hardware module is fully initialized.

also adjust the startup of your daemon, depending if it is daemonizing itself or not.

be sure that first init script /etc/rcX.d/S98first_daemon is launched before your homemade daemon startup script /etc/rcX.d/S99homemade_daemon

#!/bin/bash

wait_period=0
sleep_period=5
max_wait_period=30

function is_other_daemon_fully_initialized()
{
    lsmod | grep -qw module_name && return 0
    return 1
}

while true
do
    echo "Time Now: `date +%H:%M:%S`"
    echo "Sleeping for $sleep_period seconds"
    wait_period=$(($wait_period+$sleep_period))
    if [ $wait_period -gt $max_wait_period ];then
       echo "Max wait period exceeded, abort."
       exit 1
    else
       sleep $sleep_period
       is_other_daemon_fully_initialized && break
    fi
done

echo "Initialisation done in $wait_period seconds, launching second daemon."
/path/to/your/homemade/daemon
Chaoxiang N
  • 1,283
  • 5
  • 11
  • All daemons are started synchronously or asynchronously? if they are synchronously, I think "while" statement can cause all other daemons to start delayed? – jstuardo Dec 05 '19 at 11:29
  • sequentially. start one, then start two, then start three, ... If you search fast boot, you are not running the good distro. – Chaoxiang N Dec 05 '19 at 14:08