-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkillProcesses.js
62 lines (51 loc) · 2.24 KB
/
killProcesses.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
// Pull out command line executor
import { exec } from "child_process";
// Get command line arguments
const args = process.argv;
// If there is somethign missing or the port is not a number
if (args.length < 3 || Number.isNaN(Number(args[2]))) {
const message = `There has been an error with your command line arguments!\nArguments:\n[\n\t${args.toString().replace(/,./gi, ",\n\t")}\n]`;
throw new Error(message);
};
// Get the port
const PORT = args[2];
// Get all processes running at the port
exec(`lsof -i :${PORT}`, (error, stdout, stderr) => {
// There are no processes running at the port
if (stdout === "")
console.log(`There are no processes running on PORT ${PORT}!\nMOVING ON...\n`);
// There is an error
else if (error) {
console.log(`There was an error processing command lsof -i :${PORT}`);
throw new Error(error);
// There is no error
} else {
// Store the relevant index mapping for portProcessesList if needed
const mapping = {
0: "PROCESS_NAME",
1: "PROCESS_ID",
2: "PROCESS_USER",
3: "PROCESS_IP_TYPE",
4: "PROCESS_DEVICE_ADDRESS",
5: "PROCESS_SIZE_OFFSET",
6: "PROCESS_NODE",
7: "PROCESS_SOURCE_LOCATION"
};
// Cleans processes @ port information
let portProcessesList = stdout.split("\n").map((line, index) => line.split(" ").filter(el => el != ""));
portProcessesList = portProcessesList.slice(1, portProcessesList.length - 1).map(([ PROCESS_NAME, PROCESS_ID ]) => Number(PROCESS_ID));
// Iterate over all processes running at the port
for (const RUNNING_PROCESS_ID_AT_PORT of portProcessesList) {
// Kill the process
exec(`kill -9 ${RUNNING_PROCESS_ID_AT_PORT}`, (error, stdout, stderr) => {
// If there is an error
if (error) {
console.log(`There was an error processing command kill -9 ${RUNNING_PROCESS_ID_AT_PORT}`);
throw new Error(error);
// The processes has been killed
} else
console.log(`KILLED PROCESS ${RUNNING_PROCESS_ID_AT_PORT} @ PORT ${PORT}`);
});
};
};
});