-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop-server.js
More file actions
60 lines (53 loc) · 1.73 KB
/
stop-server.js
File metadata and controls
60 lines (53 loc) · 1.73 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
const { exec } = require('child_process');
const os = require('os');
function killProcessOnPort(port) {
const platform = os.platform();
let command;
if (platform === 'win32') {
// Windows
command = `netstat -ano | findstr :${port}`;
} else {
// Unix/Linux/Mac
command = `lsof -ti:${port}`;
}
exec(command, (error, stdout, stderr) => {
if (error) {
console.log('No process found on port', port);
return;
}
if (platform === 'win32') {
// Extract PID from Windows netstat output
const lines = stdout.split('\n');
const pids = [];
lines.forEach(line => {
const match = line.match(/\s+(\d+)\s*$/);
if (match) {
pids.push(match[1]);
}
});
pids.forEach(pid => {
exec(`taskkill /PID ${pid} /F`, (killError) => {
if (!killError) {
console.log(`Killed process ${pid} on port ${port}`);
}
});
});
} else {
// Unix/Linux/Mac
const pids = stdout.split('\n').filter(pid => pid.trim());
pids.forEach(pid => {
exec(`kill -9 ${pid}`, (killError) => {
if (!killError) {
console.log(`Killed process ${pid} on port ${port}`);
}
});
});
}
});
}
// Kill any process on port 3000
killProcessOnPort(3000);
console.log('Attempting to stop any server running on port 3000...');
setTimeout(() => {
console.log('Done. You can now start the server with "npm start"');
}, 2000);