Debian中作为守护进程的PHP脚本

Debian中作为守护进程的PHP脚本,php,linux,sockets,debian,init.d,Php,Linux,Sockets,Debian,Init.d,我似乎找不到任何能做到这一点的东西,我在谷歌上花了2个小时试图找到一个解决方案,我简直受够了。我相信这是一个简单的解决办法,但我似乎找不到 我需要运行一个位于/var/www/Game/Sockets/ChatServer.php中的.php文件作为守护进程。在基于浏览器的游戏中,此文件充当聊天系统的套接字服务器。然而,尽管尝试了很多不同的东西,我还是无法让它在开机时启动。我可以使用“service ChatServerDaemon start”来启动守护进程,但在启动时它不起作用。我在init

我似乎找不到任何能做到这一点的东西,我在谷歌上花了2个小时试图找到一个解决方案,我简直受够了。我相信这是一个简单的解决办法,但我似乎找不到

我需要运行一个位于/var/www/Game/Sockets/ChatServer.php中的.php文件作为守护进程。在基于浏览器的游戏中,此文件充当聊天系统的套接字服务器。然而,尽管尝试了很多不同的东西,我还是无法让它在开机时启动。我可以使用“service ChatServerDaemon start”来启动守护进程,但在启动时它不起作用。我在init.d中找到的文件是:

#! /bin/sh

# Installation
# - Move this to /etc/init.d/myservice
# - chmod +x this
#
# Starting and stopping
# - Start: `service myservice start` or `/etc/init.d/myservice start`
# - Stop: `service myservice stop` or `/etc/init.d/myservice stop`

#ref http://till.klampaeckel.de/blog/archives/94-start-stop-daemon,-Gearman-and-a-    little-PHP.html
#ref http://unix.stackexchange.com/questions/85033/use-start-stop-daemon-for-a-php-    server/85570#85570
#ref http://serverfault.com/questions/229759/launching-a-php-daemon-from-an-lsb-init-    script-w-start-stop-daemon

NAME=ChatServerDaemon
DESC="Chat Server Daemon for Taloren."
PIDFILE="/var/run/${NAME}.pid"
LOGFILE="/var/log/${NAME}.log"

DAEMON="/usr/bin/php"
DAEMON_OPTS="/var/www/Game/Sockets/ChatServer.php"

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 >> $LOGFILE
    echo "$NAME."
    ;;
*)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

我很生气,也厌倦了试着让它工作。有人能帮我吗。如果答案显而易见,我很抱歉/

仅仅将脚本放在
/etc/init.d/
中不足以使其在启动时运行。需要指定系统应在哪个运行级别启动或停止服务。在使用经典SysV init系统的发行版上,可以通过在特殊文件夹中创建指向init脚本的符号链接来完成

以下是确定当前运行级别的人员:

$ who -r
     run-level 2  Apr  9 10:39                   last=S
例如,下面是如何配置
cups
打印服务,以便在运行级别2中启动:

$ ls -l /etc/rc2.d/S20cups
lrwxrwxrwx 1 root root 14 Apr  6 01:24 /etc/rc2.d/S20cups -> ../init.d/cups
在运行级别2中必须启动或停止的所有内容在
/etc/rc2.d/
中都有一个符号,当必须启动服务时,符号链接的名称以
S
开头;当必须停止服务时,符号链接的名称以
K
开头,然后是两位优先级

手工处理可能会很麻烦,所以主要发行版都有自动处理的工具。在Debian或Ubuntu上,它是
更新rc.d
。在RedHat上,它是
chkconfig


另外,这个
SysV
init系统正在被
systemd
所取代(它仍然支持
SysV
init脚本)。因此,直接为
systemd
编写一个配置文件可能是值得的。或者您可以使用另一种服务管理器,如
supervisord
god
。它们更易于管理,并且具有很好的功能,例如在服务失败时自动重新启动服务。

您是只想在启动时启动作业,还是想在退出时重新启动作业?