Call to daemon in a /etc/init.d script is blocking, not running in background

﹥>﹥吖頭↗ 提交于 2019-12-03 01:21:29

I finally re-wrote the start function in the bash init script, and I am not using daemon anymore.

start() {
    echo -n "Starting $pname : "
    #daemon ${exe} # Not working ...
    if [ -s ${pidfile} ]; then
       RETVAL=1
       echo -n "Already running !" && warning
       echo
    else
       nohup ${exe} >/dev/null 2>&1 &
       RETVAL=$?
       PID=$!
       [ $RETVAL -eq 0 ] && touch ${lockfile} && success || failure
       echo
       echo $PID > ${pidfile}
    fi
}

I check that the pid file is not existing already (if so, just write a warning). If not, I use

 nohup ${exe} >/dev/null 2>&1 &

to start the script.

I don't know if it is safe this way (?) but it works.

MarkM

The proper way to daemonize a process is have it detach from the terminal by itself. This is how most larger software suites do it, for instance, apache.

The rationale behind daemon not doing what you would expect from its name, and how to make a unix process detach into the background, can be found here in section 1.7 How do I get my program to act like a daemon?

Simply invoking a program in the background isn't really adequate for these long-running programs; that does not correctly detach the process from the terminal session that started it. Also, the conventional way of starting daemons is simply to issue the command manually or from an rc script; the daemon is expected to put itself into the background.

For further reading on this topic: What's the difference between nohup and a daemon?

According to man daemon correct syntax is

daemon [options] -- [command] [command args]

Your init script startup should run something like:

daemon --pidfile ${pidfile} -- ${exe}
Thibault Deheurles

As said here, it seems that the process needs to be sent to the background using &. Daemon don’t do it for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!