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

你。 提交于 2019-12-20 09:41:14

问题


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

It works after I go to SSH, and then just manually run "php bin/chat-server.php".

What I was wondering is that, in a commercial situation, how do I keep the chat server running?

Thanks.


回答1:


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.




回答2:


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/




回答3:


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

I don't actually know about how the industry does it, as I am just a programming/linux hobbyist and student right now, but that's the route I would go on a personal server.




回答4:


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.



来源:https://stackoverflow.com/questions/19487664/how-do-i-keep-a-websocket-server-running-even-after-i-close-the-ssh-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!