diff --git a/Server/ServerManager.php b/Server/ServerManager.php index 85ae3584..913886e6 100644 --- a/Server/ServerManager.php +++ b/Server/ServerManager.php @@ -94,12 +94,32 @@ class ServerManager extends Abstracts\Server { $context = ServerManager::getContext(); foreach ($this->sortService($configs['ports']) as $config) { + if ($this->checkPort($config['port'])) { + $this->server->shutdown(); + trigger_error(sprintf('Port %s::%d is already.', $config['host'], $config['port'])); + } $this->startListenerHandler($context, $config); } $this->addServerEventCallback($this->getSystemEvents($configs)); } + /** + * @param $port + * @return bool + * @throws Exception + */ + private function checkPort($port): bool + { + if (Snowflake::getPlatform()->isLinux()) { + exec('netstat -tunlp | grep ' . $port, $output); + } else { + exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output); + } + return !empty($output); + } + + /** * @param string|CustomProcess $customProcess * @param null $redirect_stdin_and_stdout