在Plesk服务器中启动HTTP服务器时如何自动运行WebSocket(socketo.me)服务器

时间:2018-10-26 05:49:03

标签: php linux cron plesk plesk-onyx

我需要运行以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
  3. ps -aef | grep "websocket_server.php" | grep -v grep | awk '{print $2}'

  1.   

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

这是来自PuTTY终端


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

除了添加官方职位,还有其他更好的方法吗? 请尽可能为我提供解决方案。由于从腻子终端, 我能够运行命令,并且一切正常

1 个答案:

答案 0 :(得分:0)

<?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端口启动的情况下正常工作