6

I'm working on a socket server written in PHP. This part of the work is done but now I need to run it as a daemon.

For this I've tried to use start-stop-daemon but it doesn't work. My server is running Debian.

To simplify, my question is why does the following command not run my daemon or how can I debug it?

start-stop-daemon --start --quiet --background --make-pidfile --pidfile /var/run/server-ticket.pid --exec /usr/local/zend/bin/php /var/www/server/consultpilot/ServerTicket.php >> /var/log/server-ticket.log 2>> /var/log/server-ticket.log </dev/null

The following is the full script, based on Till Klampaeckel's tutorial:

#! /bin/sh

### BEGIN INIT INFO
# Provides: ServerTicket
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts ServerTicket
# Description: starts ServerTicket using start-stop-daemon
### END INIT INFO

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/zend/bin/php
DAEMON_OPTS="/var/www/server/consultpilot/ServerTicket.php"
NAME=server-ticket
DESC="Daemon for the Server Ticket from DiffMed"
PIDFILE="/var/run/${NAME}.pid"
LOGFILE="/var/log/${NAME}.log"
QUIET="--quiet"
START_OPTS="--start ${QUIET} --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} ${DAEMON_OPTS}"
STOP_OPTS="--stop --pidfile ${PIDFILE}"    

test -x $DAEMON || exit 0

set -e

case "$1" in
    start)
        echo -n "Starting $DESC: "
        start-stop-daemon $START_OPTS >> "${LOGFILE}" 2>> "${LOGFILE}" </dev/null
        echo "$NAME."       
        ;;
    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon $STOP_OPTS
        echo "$NAME."
        rm -f $PIDFILE
        ;;
    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon $STOP_OPTS
        sleep 1
        start-stop-daemon $START_OPTS
        echo "$NAME."
        ;;
*)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

For information, when I start the process, there is no return. But when I finish it, it tells me that no process corresponds:

root:/var/run$ service server-ticket start
Starting Daemon for the Server Ticket from DiffMed: result : 0
server-ticket.
root:/var/run$ service server-ticket stop
Stopping Daemon for the Server Ticket from DiffMed: start-stop-daemon: warning: failed to kill 5772: No such process
1 pids were not killed
No process in pidfile '/var/run/server-ticket.pid' found running; none killed.
  • I've also up voted the question seeing as it provided all the information I would of expected in a clear and concise way, with output from commands to illustrate the incorrect behaviour. – Drav Sloan Aug 1 '13 at 1:57
1

From the start-stop-daemon(8) man page:

-x, --exec executable Check for processes that are instances of this executable (according to /proc/pid/exe)

Which means it will check for instances of /usr/local/zend/bin/php, and if it finds them not start a new process. Provided you have the magic cookie of:

#! /usr/local/zend/bin/php

on the first line of your /var/www/server/consultpilot/ServerTicket.php script, and make sure it is executable with chmod, then you can change it to:

DAEMON=/var/www/server/consultpilot/ServerTicket.php

and get the results you would expect.

1

Thank you for your replies all !

My main problem was due to quote and so concatenation problems. For information, here is the debugged and simplified version.

#! /bin/sh

### BEGIN INIT INFO
# Provides: ServerTicket
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts ServerTicket
# Description: starts ServerTicket using start-stop-daemon
### END INIT INFO

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

DAEMON="/usr/local/zend/bin/php"
DAEMON_OPTS="/var/www/server/consultpilot/ServerTicket.php"
NAME=server-ticket
DESC="Daemon for the Server Ticket from DiffMed"
PIDFILE="/var/run/${NAME}.pid"
LOGFILE="/var/log/${NAME}.log"
START_OPTS="--start --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} ${DAEMON_OPTS}"
STOP_OPTS="--stop --pidfile ${PIDFILE}"}"

test -x $DAEMON || exit 0

set -e

case "$1" in
    start)
        echo -n "Starting ${DESC}: "
        start-stop-daemon $START_OPTS >> $LOGFILE
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon $STOP_OPTS
        echo "$NAME."
        rm -f $PIDFILE
        ;;
    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon $STOP_OPTS
        sleep 1
        start-stop-daemon $START_OPTS
        echo "$NAME."
        ;;
*)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.