-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill-server.js
142 lines (117 loc) · 3.91 KB
/
kill-server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { exec } = require('child_process');
const os = require('os');
// Function to kill processes on a specific port
function killProcessOnPort(port) {
console.log(`Attempting to kill processes on port ${port}...`);
const isWindows = os.platform() === 'win32';
if (isWindows) {
// Windows command to find and kill process
const findCommand = `netstat -ano | findstr :${port}`;
exec(findCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error finding process: ${error.message}`);
return;
}
if (stderr) {
console.error(`Error: ${stderr}`);
return;
}
if (!stdout) {
console.log(`No process found using port ${port}`);
return;
}
// Parse the output to get PIDs
const lines = stdout.split('\n').filter(line => line.trim() !== '');
const pids = new Set();
lines.forEach(line => {
const parts = line.trim().split(/\s+/);
if (parts.length > 4) {
const pid = parts[4];
pids.add(pid);
}
});
if (pids.size === 0) {
console.log(`No process found using port ${port}`);
return;
}
// Kill each process
pids.forEach(pid => {
console.log(`Killing process with PID: ${pid}`);
exec(`taskkill /F /PID ${pid}`, (killError, killStdout, killStderr) => {
if (killError) {
console.error(`Error killing process: ${killError.message}`);
return;
}
if (killStderr) {
console.error(`Error: ${killStderr}`);
return;
}
console.log(`Process with PID ${pid} killed successfully`);
});
});
});
} else {
// Unix/Linux/Mac command to find and kill process
const findCommand = `lsof -i :${port} -t`;
exec(findCommand, (error, stdout, stderr) => {
if (error && !stdout) {
console.error(`Error finding process: ${error.message}`);
return;
}
if (stderr) {
console.error(`Error: ${stderr}`);
return;
}
if (!stdout) {
console.log(`No process found using port ${port}`);
return;
}
// Parse the output to get PIDs
const pids = stdout.split('\n').filter(pid => pid.trim() !== '');
if (pids.length === 0) {
console.log(`No process found using port ${port}`);
return;
}
// Kill each process
pids.forEach(pid => {
console.log(`Killing process with PID: ${pid}`);
exec(`kill -9 ${pid}`, (killError, killStdout, killStderr) => {
if (killError) {
console.error(`Error killing process: ${killError.message}`);
return;
}
if (killStderr) {
console.error(`Error: ${killStderr}`);
return;
}
console.log(`Process with PID ${pid} killed successfully`);
});
});
});
}
}
// Kill processes on port 3001
killProcessOnPort(3001);
// Also check for any node processes that might be running the server
const isWindows = os.platform() === 'win32';
if (isWindows) {
exec('tasklist /FI "IMAGENAME eq node.exe" /FO CSV', (error, stdout, stderr) => {
if (error || stderr) {
console.error('Error checking for Node processes:', error || stderr);
return;
}
console.log('Node processes currently running:');
console.log(stdout);
console.log('To kill a specific Node process, use: taskkill /F /PID <PID>');
});
} else {
exec('ps aux | grep node', (error, stdout, stderr) => {
if (error) {
console.error('Error checking for Node processes:', error);
return;
}
console.log('Node processes currently running:');
console.log(stdout);
console.log('To kill a specific Node process, use: kill -9 <PID>');
});
}