-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (54 loc) · 1.84 KB
/
script.js
File metadata and controls
59 lines (54 loc) · 1.84 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
const { exec } = require('child_process');
// Function to kill all Node.js processes except the current one
function killAllNodeProcesses() {
// Get the process ID of the current Node.js process
const currentProcessId = process.pid;
console.log({ currentProcessId });
// Find all Node.js processes except the current one
exec('pgrep node', (error, stdout, stderr) => {
if (error) {
console.error(`Error occurred: ${error.message}`);
return;
}
if (stderr) {
console.error(`Command execution failed: ${stderr}`);
return;
}
// Split the output into lines and process each line
const processList = stdout.split('\n');
processList.forEach((processId) => {
if (processId) {
// Skip killing the current process
if (parseInt(processId, 10) !== currentProcessId) {
exec(`kill ${processId}`, (killError, killStdout, killStderr) => {
if (killError) {
console.error(`Error occurred while killing process ${processId}: ${killError.message}`);
} else {
console.log(`Process ${processId} terminated.`);
}
});
}
}
});
});
}
// Execute the system command to get the list of running Node.js processes
exec('pgrep -l node', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
return;
}
if (stderr) {
console.error(`Command execution error: ${stderr}`);
return;
}
// Split the output into lines and process each line
const processList = stdout.split('\n');
const nodeProcesses = processList.filter((process) => process.includes('node'));
if (nodeProcesses.length > 1) {
console.log('Killing all processes except the current one.');
killAllNodeProcesses();
} else {
console.log('No running instances found.');
}
});