Erstellen Sie einen Daemon.
Wenn Sie Symfony2 verwenden, können Sie die Process Component verwenden.
// 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";
}
Wenn Sie nativen PHP verwenden, keine Angst, popen
ist dein Freund!
// 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";
}
Es gibt natürlich auch andere hilfreiche PHP-Bibliotheken für die Arbeit mit Daemons. Googling "php daemon" wird Ihnen eine Menge Hinweise geben.
Dieses Tutorial zeigt eine wirklich coole Art, die WebSocket in einem * nix-Dienst des Drehens zu machen, bestehen Selbst wenn Sie Ihre SSH-Verbindung schließen. http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/ – MarshallOfSound