Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·101 lines (86 loc) · 3.12 KB
/
Copy pathindex.js
File metadata and controls
executable file
·101 lines (86 loc) · 3.12 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
'use strict';
require('shelljs/global');
const co = require('co');
const prompt = require('co-prompt');
const chalk = require('chalk');
const program = require('commander');
var isWin = /^win/.test(process.platform);
program.arguments('<port>')
.option('-k, --kill', 'Tries to force kill a process using this port.')
.action(function(port) {
co(function *() {
let command;
if (isWin) {
command = `netstat -aon | find ":${port} " | find "LISTENING"`;
} else {
command = `lsof -i :${port} -n -P | grep 'LISTEN' | awk '{ print $2; }' | head -n 2 | grep -v PID`;
}
const res = exec(command, { silent: true });
if (res.code === 0) {
let pid = res.stdout.trim();
if (isWin) {
let lines = pid.split(/\r\n/g);
pid = [];
for(var line of lines) {
let pinfo = line.split(/\s+/g);
let id = pinfo.pop();
if(pid.indexOf(id) == -1)
pid.push(id);
}
} else {
pid = pid.split(/\s+/g);
}
const pids = Array.from(new Set(pid)); // remove duplicates
const pnames = [];
for (let i = 0; i < pids.length; i++) {
const pid = pids[i];
if (!isWin) {
const ps = exec(`ps -p ${pid} -o comm=`, { silent: true });
if (ps.code === 0) {
const pname = ps.stdout.trim();
pnames.push(pname);
} else {
pnames.push('N/A');
}
}
}
console.log(
`PID: ${chalk.blue.bold(pids.join(', '))}\n` +
(!isWin ? `Process name: ${chalk.green.bold(pnames.join(', '))}\n` : '') +
`Listening on port: ${chalk.red.bold(port)}`
);
if (!program.kill) {
program.kill = yield prompt.confirm(`Do you want to kill the process? (no = default | y): `);
process.stdin.pause();
}
if (program.kill) {
console.log(`Trying to kill process...`);
let killResult;
if (isWin) {
killResult = exec(`taskkill /PID ${pids.join(' /PID ')} /T /F`);
} else {
killResult = exec(`kill -9 ${pids.join(' ')}`);
}
if (killResult.code === 0) {
console.log(chalk.green('Process(es) killed.'));
} else {
console.log(chalk.yellow('Process(es) couldn\'t be killed. Maybe you need to try as', (isWin?'administrator':'root') + '.'));
}
}
} else if (res.code === 1){
console.log("No process found which uses port " + port + ". Maybe you need to try as root.");
}
});
})
.on('--help', function(){
if (isWin) {
// we could show windows specific information here
} else {
console.log(' HINT: If no process can be found which listens for a port, but you\'re sure one is using it, you can use "sudo whoport {port}" to check processes for all users. You might also need to invoke "sudo whoport {port} -k" to kill a process.\n');
}
})
.parse(process.argv);
if (typeof(program.args[0]) === 'undefined') {
program.help();
}