start-stop-daemon doesn't detect already running daemon

1.7k views Asked by At

I tried to copy /etc/init.d/skeleton and modify it to start a program in background. I came up with the following:

do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE -b --make-pidfile --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE -b --make-pidfile --exec $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
}

do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

I have a problem and several questions about this. My problem is, that the start function starts a daemon and returns 0 regardless of wether the daemon was already running. My question is if -b and --make-pidfile is required at the first --test start. And am I right with the assumption that the first command in do_stop sends a TERM signal to the daemon and the second command kills the daemon forcefully? In this case I would have to wait on the daemon to quit.

1

There are 1 answers

0
timakro On BEST ANSWER

My problem was --exec because my script is an interpreted script start-stop-daemon couldn't find a program with this name running since the interpreter was only running. I fixed this by using --startas except for --exec.