1

我需要运行以 PHP 24/7/365 编写的 PHP WebSocket

名为 websocket_server.php 的脚本的路径位于以下路径中

/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/websocket_server.php

为了首先运行上述脚本,它需要注册为服务并自动调用此命令。我对在 Linux(Ubuntu 16.04)中设置服务知之甚少。所以我开始设置皇冠作业调度程序。但由于错误,它没有产生任何结果。

为了在任务调度程序中运行,我添加了三个命令,它们如下:

  1. @reboot root nohup php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php 2>&1 >/dev/null &

    
    Task "@reboot root nohup php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/websocket_server.php 2>&1 >/dev/null &" successfully completed in 0 seconds, output:
    -: @reboot: command not found
    

  2. httpdocs/proj_ci/application/libraries/server/websocket_launch.sh 带参数 cron:run

    
      Task "httpdocs/proj_ci/application/libraries/server/websocket_launch.sh" successfully completed in 0 seconds, output:
      PID=ps -aef | grep "websocket_server.php" | grep -v grep | awk '{print $2}'
      if [ -z $PID ]
      then

    #echo "Launching now" nohup php websocket_server.php > error_log & else # echo "Running as PID $PID" fi

  1. /usr/bin/php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php


Task "/usr/bin/php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php" completed with error in 0 seconds, output:
Could not open input file: /usr/bin/php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php

这是来自 PuTTY 终端

root@h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries# php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php
PHP Warning:  require_once(../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php on line 10
PHP Fatal error:  require_once(): Failed opening required '../vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php on line 10
root@h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries# cd /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server
root@h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server# php websocket_server.php                                             ^C
root@h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server#
root@h3069528:/var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server#

除了增加皇冠职位,还有什么更好的方法吗?请尽可能帮助我解决问题。由于从 PuTTY 终端,我可以运行命令并且一切正常

4

1 回答 1

1
<?php 
# The maximum execution time, in seconds. If set to zero, no time limit is imposed.
set_time_limit(0);

# Make sure to keep alive the script when a client disconnect.
ignore_user_abort(true);

error_reporting(E_ALL);
ini_set('display_errors', 'On');
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "/var/www/vhosts/abc.xy/httpdocs/websocket.log");

echo "Script start at: " . date('h:i:s') . "\n"; 
exec('bash -c "exec nohup php websocket_server.php >> /var/www/vhosts/abc.xy/httpdocs/websocket.log 2>&1 &"');
shell_exec('nohup php /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/server/websocket_server.php 2>&1 >> /var/www/vhosts/abc.xy/httpdocs/websocket.log &');
echo "Script end at: " . date('h:i:s'); 
?>

这会将所有日志保存在给定的指定路径中/var/www/vhosts/abc.xy/httpdocs/websocket.log

如果您从浏览器按名称访问指定的脚本,则上述脚本可以正常工作。如果服务器重新启动,您将能够在 Cron 作业中安排它

万一该exec()方法在某些托管服务器中失败,shell_exec()如果它已经在端口 8080 中启动,它将起作用而不影响它

于 2018-10-27T05:35:19.637 回答