Search code examples
phplinuxdaemonstart-stop-daemon

php and error "start-stop-daemon: unable to stat"


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"
}

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