How to save websocket server even after closing SSH terminal?

So, I am using Ratchet with PHP and have currently downloaded the successful websocket example to my server.

It works after I switch to SSH and then just start manually "php bin / chat-server.php".

What was interesting to me was in a commercial situation, how can I make the chat server work?

Thanks.

+6
source share
4 answers

Create 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 running?\n"; } 

If you use your own PHP, never be afraid, 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 running?\n"; } 

Of course, there are other useful PHP libraries for working with daemons. Googling "php daemon" will give you a lot of pointers.

+6
source

This tutorial shows a really cool way to turn WebSocket into a * nix service so that it persists even when you close your SSH connection.

Basically you create a /etc/init/socket.conf file 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  (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/

+1
source

Run it in /etc/rc.d/rc for * nix servers. This should run your PHP script whenever the server boots.

I really don’t know how the industry does this, since I am just a programmer / linux hobbyist and student right now, but that route I would go to a personal server.

0
source

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

Old answer: This might be a bad idea on a prod server (this is a personal guess), but you can use the screen command to open a terminal, start your daemon, and then press Ctrl-A, Ctrl-D and your terminal will still be alive opened in the background. To reconnect to this terminal, connect to the server and enter screen -r .

-1
source

Source: https://habr.com/ru/post/956383/


All Articles