php – 即使关闭SSH终端后,如何保持websocket服务器运行?

前端之家收集整理的这篇文章主要介绍了php – 即使关闭SSH终端后,如何保持websocket服务器运行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我正在使用Ratchet with PHP,并且目前已将一个成功的websocket示例上传到我的服务器.

它在我转到SSH之后工作,然后只需手动运行“PHP bin / chat-server.PHP”.

我想知道的是,在商业环境中,如何让聊天服务器保持运行?

谢谢.

做一个守护进程.

如果您使用的是symfony2,则可以使用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";
}

如果您使用的是原生PHP,请不要担心,popen是您的朋友!

// 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/',SIGKILL);
        $stopped = True;
    }
}
if ($stopped) {
    echo "Server stopped.\n";
} else {
    echo "Server not found. Are you sure it's running?\n";
}

当然还有其他有用的PHP库用于处理守护进程.谷歌搜索PHP守护进程”将给你很多指示.

原文链接:https://www.f2er.com/php/134261.html

猜你在找的PHP相关文章