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.
source share