@alanlee97/kill-port-process 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -0
- package/bin/cli.js +65 -0
- package/index.js +97 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @alanlee97/kill-port-process
|
|
2
|
+
|
|
3
|
+
一个用于根据端口号杀掉对应进程的 CLI 工具,支持 Windows 和 macOS。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @alanlee97/kill-port-process
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用方法
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
kpp <port>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### 示例
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# 杀掉占用 3000 端口的进程
|
|
21
|
+
kpp 3000
|
|
22
|
+
|
|
23
|
+
# 杀掉占用 8080 端口的进程
|
|
24
|
+
kpp 8080
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 命令选项
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
kpp --help # 显示帮助信息
|
|
31
|
+
kpp --version # 显示版本号
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 跨平台支持
|
|
35
|
+
|
|
36
|
+
- **Windows**: 使用 `netstat` 查找进程,`taskkill` 终止进程
|
|
37
|
+
- **macOS**: 使用 `lsof` 查找进程,`kill` 终止进程
|
|
38
|
+
- **Linux**: 使用 `lsof` 查找进程,`kill` 终止进程
|
|
39
|
+
|
|
40
|
+
## 输出示例
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
$ kpp 3000
|
|
44
|
+
Successfully killed process(es) on port 3000. PIDs: 12345
|
|
45
|
+
|
|
46
|
+
$ kpp 8080
|
|
47
|
+
No process found running on port 8080.
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 系统要求
|
|
51
|
+
|
|
52
|
+
- Node.js >= 14.0.0
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
MIT
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { killPortProcess } = require('../index.js');
|
|
4
|
+
|
|
5
|
+
function showHelp() {
|
|
6
|
+
console.log(`
|
|
7
|
+
Usage: kpp <port>
|
|
8
|
+
|
|
9
|
+
Kill process running on the specified port.
|
|
10
|
+
|
|
11
|
+
Options:
|
|
12
|
+
-h, --help Show help information
|
|
13
|
+
-v, --version Show version number
|
|
14
|
+
|
|
15
|
+
Examples:
|
|
16
|
+
kpp 3000 Kill process on port 3000
|
|
17
|
+
kpp 8080 Kill process on port 8080
|
|
18
|
+
`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function showVersion() {
|
|
22
|
+
const pkg = require('../package.json');
|
|
23
|
+
console.log(pkg.version);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function main() {
|
|
27
|
+
const args = process.argv.slice(2);
|
|
28
|
+
|
|
29
|
+
if (args.length === 0) {
|
|
30
|
+
console.error('Error: Please provide a port number.');
|
|
31
|
+
showHelp();
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const arg = args[0];
|
|
36
|
+
|
|
37
|
+
if (arg === '-h' || arg === '--help') {
|
|
38
|
+
showHelp();
|
|
39
|
+
process.exit(0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (arg === '-v' || arg === '--version') {
|
|
43
|
+
showVersion();
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const port = parseInt(arg, 10);
|
|
48
|
+
|
|
49
|
+
if (isNaN(port) || port <= 0 || port > 65535) {
|
|
50
|
+
console.error('Error: Please provide a valid port number (1-65535).');
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const result = killPortProcess(port);
|
|
55
|
+
|
|
56
|
+
if (result.success) {
|
|
57
|
+
console.log(result.message);
|
|
58
|
+
process.exit(0);
|
|
59
|
+
} else {
|
|
60
|
+
console.error(result.message);
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
main();
|
package/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const { execSync } = require('child_process');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
|
|
4
|
+
function getProcessIdByPort(port) {
|
|
5
|
+
const platform = os.platform();
|
|
6
|
+
let command;
|
|
7
|
+
let pidList = [];
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
if (platform === 'win32') {
|
|
11
|
+
command = `netstat -ano | findstr :${port}`;
|
|
12
|
+
const output = execSync(command, { encoding: 'utf-8' });
|
|
13
|
+
const lines = output.trim().split('\n');
|
|
14
|
+
|
|
15
|
+
for (const line of lines) {
|
|
16
|
+
const parts = line.trim().split(/\s+/);
|
|
17
|
+
if (parts.length >= 5) {
|
|
18
|
+
const localAddress = parts[1];
|
|
19
|
+
const pid = parts[4];
|
|
20
|
+
if (localAddress.includes(`:${port}`) && pid !== '0') {
|
|
21
|
+
pidList.push(pid);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
command = `lsof -ti :${port}`;
|
|
27
|
+
const output = execSync(command, { encoding: 'utf-8' });
|
|
28
|
+
pidList = output.trim().split('\n').filter(pid => pid);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return [...new Set(pidList)];
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function killProcess(pid) {
|
|
38
|
+
const platform = os.platform();
|
|
39
|
+
let command;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
if (platform === 'win32') {
|
|
43
|
+
command = `taskkill /PID ${pid} /F`;
|
|
44
|
+
} else {
|
|
45
|
+
command = `kill -9 ${pid}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
execSync(command, { encoding: 'utf-8' });
|
|
49
|
+
return true;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function killPortProcess(port) {
|
|
56
|
+
if (!port || isNaN(port)) {
|
|
57
|
+
return {
|
|
58
|
+
success: false,
|
|
59
|
+
message: 'Please provide a valid port number.'
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const pidList = getProcessIdByPort(port);
|
|
64
|
+
|
|
65
|
+
if (pidList.length === 0) {
|
|
66
|
+
return {
|
|
67
|
+
success: false,
|
|
68
|
+
message: `No process found running on port ${port}.`
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const results = [];
|
|
73
|
+
for (const pid of pidList) {
|
|
74
|
+
const killed = killProcess(pid);
|
|
75
|
+
results.push({ pid, killed });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const killedCount = results.filter(r => r.killed).length;
|
|
79
|
+
|
|
80
|
+
if (killedCount === pidList.length) {
|
|
81
|
+
return {
|
|
82
|
+
success: true,
|
|
83
|
+
message: `Successfully killed process(es) on port ${port}. PIDs: ${pidList.join(', ')}`
|
|
84
|
+
};
|
|
85
|
+
} else {
|
|
86
|
+
return {
|
|
87
|
+
success: false,
|
|
88
|
+
message: `Partially killed processes on port ${port}. Success: ${killedCount}/${pidList.length}`
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = {
|
|
94
|
+
killPortProcess,
|
|
95
|
+
getProcessIdByPort,
|
|
96
|
+
killProcess
|
|
97
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alanlee97/kill-port-process",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A CLI tool to kill processes by port number",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"kpp": "./bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js",
|
|
11
|
+
"bin/",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"kill",
|
|
19
|
+
"port",
|
|
20
|
+
"process",
|
|
21
|
+
"cli",
|
|
22
|
+
"tool"
|
|
23
|
+
],
|
|
24
|
+
"author": "alanlee97",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=14.0.0"
|
|
28
|
+
},
|
|
29
|
+
"os": [
|
|
30
|
+
"win32",
|
|
31
|
+
"darwin",
|
|
32
|
+
"linux"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
}
|
|
37
|
+
}
|