-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupt-handler.js
52 lines (40 loc) · 1.23 KB
/
interrupt-handler.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
const { exec } = require("child_process");
// Capturar os comandos passados como argumentos
const mainCommand = process.argv[2];
const cleanupCommand = process.argv[3];
if (!mainCommand) {
console.error("Nenhum comando principal foi fornecido.");
process.exit(1);
}
if (!cleanupCommand) {
console.error("Nenhum comando de limpeza foi fornecido.");
process.exit(1);
}
console.log(`Executando comando principal: ${mainCommand}`);
// Executar o comando principal
const mainProcess = exec(mainCommand);
mainProcess.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
mainProcess.stderr.on("data", (data) => {
console.error(`stderr: ${data}`);
});
mainProcess.on("close", (code) => {
console.log(`Processo principal terminou com código ${code}`);
});
// Capturar SIGINT (Ctrl+C) e executar o comando de limpeza
process.on("SIGINT", () => {
console.log(
"Sinal de interrupção capturado, executando comando de limpeza...",
);
const cleanupProcess = exec(cleanupCommand);
cleanupProcess.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
cleanupProcess.stderr.on("data", (data) => {
console.error(`stderr: ${data}`);
});
cleanupProcess.on("close", () => {
process.exit();
});
});