How do I keep a websocket server running, even after I close the SSH terminal?

The ratchet documentation has a deploy page. Did you checked it?

Old answer: It may be a bad idea on a prod server (this is a personal assumption), but you can use the screen command to open a terminal, launch your daemon, then press Ctrl-A, Ctrl-D, and your terminal is still alive, opened in background. To reconnect to this terminal, connect back to your server and type screen -r.


From the Ratchet Deployment page:

if you are running Ubuntu, fallow these steps:

  1. install supervisor like this: Installing Supervisor

  2. configure ratchet webserver as a service program like this: Supervisor Process Control | Supervisord Install & Usage

making a conf file in /etc/supervisor/conf.d/.conf and fill the conf exmple code from Ratchet Deployment page:

[program:ratchet]
command                 = bash -c "ulimit -n 10000; exec /usr/bin/php /absolute/path/to/ratchet-server-file.php)"
process_name            = Ratchet
numprocs                = 1
autostart               = true
autorestart             = true
user                    = root
stdout_logfile          = ./logs/info.log
stdout_logfile_maxbytes = 1MB
stderr_logfile          = ./logs/error.log
stderr_logfile_maxbytes = 1MB
  1. run the following comands:

    • $ supervisorctl reread
    • $ supervisorctl update
    • and finally check if your service is running: $ supervisorctl

these are all the steps that should be added in Ratched Deployment tutorial.. but this approach may not be the best..


This tutorial shows a really cool way of turning the WebSocket into a *nix Service to make it persist even when you close your SSH connection.

Basically you make a file /etc/init/socket.conf with the following contents

# Info
description "Runs the Web Socket"  
author      "Your Name Here"

# Events
start on startup  
stop on shutdown

# Automatically respawn
respawn  
respawn limit 20 5

# Run the script!
# Note, in this example, if your PHP script (the socket) returns
# the string "ERROR", the daemon will stop itself.
script  
    [ $(exec /usr/bin/php -f /path/to/socket.php) = 'ERROR' ] && ( stop; exit 1; )
end script  

Blog Post:
http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/


Make a daemon.

If you are using symfony2, you can use the Process Component.

// in your server start command
$process = new Process('/usr/bin/php bin/chat-server.php');
$process->start();
sleep(1);
if ($process->isRunning()) {
    echo "Server started.\n";
} else {
    echo $process->getErrorOutput();
}

// in your server stop command
$process = new Process('ps ax | grep bin/chat-server.php');
$process->run();
$output = $process->getOutput();
$lines = preg_split('/\n/', $output);
// kill everything (there can be multiple processes if they are spawned)
$stopped = False;
foreach ($lines as $line) {
    $ar = preg_split('/\s+/', trim($line));
    if (in_array('/usr/bin/php', $ar)
        and in_array('bin/chat-server.php', $ar)) {
        $pid = (int) $ar[0];
        posix_kill($pid, SIGKILL);
        $stopped = True;
    }
}
if ($stopped) {
    echo "Server stopped.\n";
} else {
    echo "Server not found. Are you sure it's running?\n";
}

If you are using native PHP, never fear, popen is your friend!

// in your server start command
_ = popen('/usr/bin/php bin/chat-server.php', 'r');
echo "Server started.\n";

// in your server stop command
$output = array();
exec('ps ax | grep bin/chat-server.php', &$output);
$lines = preg_split('/\n/', $output);
// kill everything (there can be multiple processes if they are spawned)
$stopped = False;
foreach ($lines as $line) {
    $ar = preg_split('/\s+/', trim($line));
    if (in_array('/usr/bin/php', $ar)
        and in_array('bin/chat-server.php', $ar)) {
        $pid = (int) $ar[0];
        posix_kill($pid, SIGKILL);
        $stopped = True;
    }
}
if ($stopped) {
    echo "Server stopped.\n";
} else {
    echo "Server not found. Are you sure it's running?\n";
}

There are of course also other helpful PHP libraries for working with daemons. Googling "php daemon" will give you a lot of pointers.

Tags:

Php

Ssh

Ratchet