-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (65 loc) · 2.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
const { exec } = require('child_process');
// 获取命令行参数
const args = process.argv.slice(2);
if (args.length < 1) {
console.error('Usage: kill-port <port1> [<port2> ...]');
process.exit(1);
}
const ports = args.map(port => parseInt(port, 10)).filter(port => !isNaN(port));
if (ports.length === 0) {
console.error('Invalid port number(s)');
process.exit(1);
}
const findPidCommand = port => process.platform === 'win32'
? `netstat -ano | findstr :${port}`
: `lsof -i:${port} -t`;
const killCommand = pid => process.platform === 'win32'
? `taskkill /PID ${pid} /F`
: `kill -9 ${pid}`;
const findAndKillProcess = (port) => {
return new Promise((resolve, reject) => {
// 查找占用指定端口的进程ID
exec(findPidCommand(port), (err, stdout, stderr) => {
if (err || stderr) {
resolve(`Port ${port} is not in use.`);
return;
}
const pids = process.platform === 'win32'
? stdout
.split('\n')
.filter(line => line.includes('LISTEN'))
.map(line => line.trim().split(/\s+/).pop())
.filter(Boolean)
: stdout.split('\n').map(line => line.trim()).filter(Boolean);
if (pids.length === 0) {
resolve(`No process found on port ${port}`);
return;
}
console.log(`Found processes on port ${port} with PIDs: ${pids.join(', ')}`);
Promise.all(
pids.map(pid =>
new Promise((killResolve, killReject) => {
exec(killCommand(pid), (killErr, killStdout, killStderr) => {
if (killErr || killStderr) {
killReject(`Error killing process ${pid}: ${killErr || killStderr}`);
return;
}
killResolve(`Process ${pid} killed successfully on port ${port}.`);
});
})
)
)
.then(killResults => resolve(killResults.join('\n')))
.catch(killErr => reject(killErr));
});
});
};
// 并行处理所有端口
Promise.all(ports.map(findAndKillProcess))
.then(results => {
results.forEach(result => console.log(result));
})
.catch(err => {
console.error(err);
});