From 5c1282959cfb155824e3f009ed67ac290409956a Mon Sep 17 00:00:00 2001 From: whwyy Date: Tue, 7 Jul 2026 22:59:28 +0800 Subject: [PATCH] eee --- function.php | 74 +++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/function.php b/function.php index fa6f41aa..f23c4b60 100644 --- a/function.php +++ b/function.php @@ -212,39 +212,47 @@ if (!function_exists('call')) { } -if (!function_exists('checkPortIsAlready')) { - - - /** - * @param $port - * @return bool|string - * @throws - */ - function checkPortIsAlready($port): bool|string - { - if (!Kiri::getPlatform()->isLinux()) { - exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output); - if (empty($output)) - return FALSE; - $output = explode(PHP_EOL, $output[0]); - return $output[0]; - } - - $serverPid = file_get_contents(storage('.swoole.pid')); - if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) { - Process::kill($serverPid, 0) && Process::kill($serverPid, SIGTERM); - } - - exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output); - if (empty($output)) { - return FALSE; - } - return explode('/', $output[0])[0]; - } - -} - - +if (!function_exists('checkPortIsAlready')) { + + + /** + * @param $port + * @return bool|string + */ + function checkPortIsAlready($port): bool|string + { + $port = (int)$port; + if ($port <= 0) { + return FALSE; + } + + if (!Kiri::getPlatform()->isLinux()) { + exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output); + if (empty($output)) { + return FALSE; + } + $output = explode(PHP_EOL, $output[0]); + return $output[0]; + } + + exec('netstat -lnp 2>/dev/null | grep :' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output); + if (empty($output)) { + exec('ss -ltnp 2>/dev/null | grep :' . $port . ' | sed -n \"s/.*pid=\\([0-9]*\\).*/\\1/p\"', $output); + } + if (empty($output)) { + return FALSE; + } + + $pid = trim((string)$output[0]); + if (str_contains($pid, '/')) { + $pid = explode('/', $pid)[0]; + } + + return $pid !== '' ? $pid : FALSE; + } + +} + if (!function_exists('set_env')) {