How to run web socket scripts coded in PHP in the Plesk web hosting control panel. I need to run the PHP WebSocket scripted in PHP 24/7/365. The path of the script named websocket_server.php lies in the path: /var/www/vhosts/abc.xy/httpdocs/proj_ci/application/libraries/websocket_server.php

In order to run the above script first, it needs to register as a service and invoke this command automatically. I didn't have much knowledge about setting up the service in Linux (Ubuntu 16.04). So I had gone with setting up the cron job scheduler. But it didn't yield any results because of errors.

I had added three commands in order to run in task scheduler they are as follows:

  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 > with arguments 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 ]
    ####\#echo "Launching now" nohup php websocket_server.php > error_log & else # echo "Running as PID $PID" fi
  3. /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

This is from the PuTTY terminal:

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

Apart from adding cron jobs are there any better ways to do so? Please help me with the solution as much as possible. Since from the PuTTY terminal, I was able to run the command and everything works fine.

# The maximum execution time, in seconds. If set to zero, no time limit is imposed.

# Make sure to keep alive the script when a client disconnect.

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'); 

This will keep all the logs in a specified path given /var/www/vhosts/abc.xy/httpdocs/websocket.log

The above script works fine if you access the specified script by name from the browser. In case if the server restarts you'll be able to schedule it in the Cron job

In case if the exec() method fails in some hosting server shell_exec() will work without affecting in case if it's already started in port 8080

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.