mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-14 11:12:51 +00:00
优化端口判断
This commit is contained in:
parent
230b989415
commit
921acf2ce0
@ -8,14 +8,19 @@ import { execSync } from 'child_process';
|
|||||||
function isPortAvailableSync(port: number): boolean {
|
function isPortAvailableSync(port: number): boolean {
|
||||||
try {
|
try {
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
// Windows 使用 netstat 检查端口
|
// Windows 使用 netstat 检查端口,排除 TIME_WAIT 状态
|
||||||
execSync(`netstat -ano | findstr :${port}`, { stdio: 'ignore' });
|
const result = execSync(`netstat -ano | findstr :${port}`, {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
});
|
||||||
|
// 如果端口只处于 TIME_WAIT 状态,则认为端口可用
|
||||||
|
return !result || result.toLowerCase().includes('time_wait');
|
||||||
} else {
|
} else {
|
||||||
// Linux/Mac 使用 lsof 检查端口
|
// Linux/Mac 使用 lsof 检查端口,只检查 LISTEN 状态
|
||||||
execSync(`lsof -i :${port}`, { stdio: 'ignore' });
|
const result = execSync(`lsof -i :${port} -sTCP:LISTEN`, {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
});
|
||||||
|
return !result;
|
||||||
}
|
}
|
||||||
// 命令执行成功,端口被占用
|
|
||||||
return false;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 命令执行失败,端口可用
|
// 命令执行失败,端口可用
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user