Question

I have a daemon that I have created using php. I want to have this called by initscripts and have it start on boot, which works fine. However, when I try to kill the process using

sudo service crystal_send stop

it does not kill the process.

And when I call this directly

start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile /var/run/crystal/crystal_send.pid --exec /bin/crystal_send  

I get

start-stop-daemon: unable to stat /bin/crystal_send  (No such file or directory)

Here is what my /etc/init.d/crystal_send do_stop function looks like.

## /etc/init.d/crystal_send
NAME=crystal_send
DAEMON=/bin/$NAME
PIDFILE=/var/run/crystal/$NAME.pid

....


do_stop()       
{  
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON  
    RETVAL="$?"
    rm -f $PIDFILE
    [ "$RETVAL" = 2 ] && return 2

    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    rm -f $PIDFILE
    return "$RETVAL"
}
Was it helpful?

Solution

After looking at dostrander's code and my code again I realize that in neither case would the pidfile or the lockfile be removed if the call to start-stop-daemon is successful. It might be better to remove them before calling start-stop-daemon. But after checking that the PID is correct.

        if [ $(sed -n '1p' < $PIDFILE) == $(pidof -x $NAME) ]; then
            rm -f $PIDFILE
            rm -f $LOCKFILE
    fi

    start-stop-daemon -K -q --retry=TERM/30/KILL/5 -n $NAME
    RETVAL="$?"
    printf "RETVAL is $RETVAL.\n"

    [ "$RETVAL" = 4 ] && return 4

OTHER TIPS

Well come to find out the problem has to do with the start-stop-daemon --stop, in particular the
--exec $DAEMON
part was not what I should have been using and I should have been using
--name $NAME. This because --exec is looking for a command and the daemon was written in php (an interpreted language) therefor it is actually calling php, which then calls my php program (crystal_send), and since start-stop-daemon is looking for a command /bin/crystal_send instead of /bin/php /bin/crystal_send it won't find it.

Therefor you should have start-stop-daemon look for the name in the process table, this is what i am doing when I use --name $NAME.

So my final do_stop function looks like this

## /etc/init.d/crystal_send
NAME=crystal_send
DAEMON=/bin/$NAME
PIDFILE=/var/run/crystal/$NAME.pid

....


do_stop()       
{  
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME  
    RETVAL="$?"
    rm -f $PIDFILE
    [ "$RETVAL" = 2 ] && return 2

    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --name $NAME
    [ "$?" = 2 ] && return 2
    rm -f $PIDFILE
    return "$RETVAL"
}

dostrander,

Thanks this poisting. It helped me solve some problems I was having. But I wonder if

do_stop()       

{
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?" rm -f $PIDFILE [ "$RETVAL" = 4 ] && return 4

start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --name $NAME
[ "$?" = 4 ] && return 4
rm -f $PIDFILE
rm -f /var/lock/$NAME

return "$RETVAL"

}

would be better?

Doesn't a 2 mean the program is dead and /var/lock lock file exists

Also I cannot find anywhere in /lib/lsb/init-functions that killproc() would return a 2.

Whereas a 4 means program or service status is unknown

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top