对 PHP 服务器使用 start-stop-daemon

对 PHP 服务器使用 start-stop-daemon

我正在开发一个用 PHP 编写的套接字服务器。这部分工作已经完成,但现在我需要将其作为守护进程运行。

为此我尝试使用start-stop-daemon但它不起作用。我的服务器运行的是 Debian。

为了简单起见,我的问题是为什么以下命令不运行我的守护程序或者如何调试它?

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

以下是完整的脚本,基于直到 Klampaeckel 的教程:

#! /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

仅供参考,当我开始该过程时,没有返回。但是当我完成它时,它告诉我没有进程对应:

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.

答案1

start-stop-daemon(8)手册页:

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

这意味着它将检查 的实例/usr/local/zend/bin/php,如果发现它们,则不会启动新进程。假设您有以下神奇 cookie:

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

在 /var/www/server/consultpilot/ServerTicket.php 脚本的第一行,并确保它可以使用 执行chmod,然后您可以将其更改为:

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

并获得您期望的结果。

答案2

谢谢大家的回复!

我的主要问题是由于引用和串联问题。有关信息,这里是调试和简化版本。

#! /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

相关内容