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
// Begin reading from stdin so the process does not exit.
process.stdin.resume();

// only works when there is no task running
// because we have a server always listening port, this handler will NEVER execute
process.on("beforeExit", (code) => {
console.log("Process beforeExit event with code: ", code);
});

// only works when the process normally exits
// on windows, ctrl-c will not trigger this handler (it is unnormal)
// unless you listen on 'SIGINT'
process.on("exit", (code) => {
console.log("Process exit event with code: ", code);
});

// can usually be generated with Ctrl+C (though this may be configurable). It is not generated when terminal raw mode is enabled and Ctrl+C is used.
process.on("SIGINT", (signal) => {
console.log(`Process ${process.pid} has been interrupted`);
process.exit(0);
});

// reserved by Node.js to start the debugger. It's possible to install a listener but doing so might interfere with the debugger.
process.on('SIGUSR1', (signal) => {
console.log(`Process ${process.pid} received a SIGUSR1 signal`);
});

// on Windows when the console window is closed, and on other platforms under various similar conditions.
process.on('SIGHUP', (signal) => {
console.log(`Process ${process.pid} received a SIGHUP signal`);
});

// when the console has been resized. On Windows, this will only happen on write to the console when the cursor is being moved, or when a readable tty is used in raw mode.
process.on('SIGWINCH', (signal) => {
console.log(`Process ${process.pid} received a SIGWINCH signal`);
});

// cannot have a listener installed, it will unconditionally terminate Node.js on all platforms.
process.on('SIGKILL', (signal) => {
console.log(`Process ${process.pid} received a SIGKILL signal`);
});


// what about errors
// try remove/comment this handler, 'exit' event still works
process.on("uncaughtException", (err) => {
console.log(`Uncaught Exception: ${err.message}`);
process.exit(1);
});