Node.js Process Signal Events

25 Oct, 2021
Signals are a POSIX (The Portable Operating System Interface) intercommunication system. These events will be emitted when Node receives signal events. In order to notify that an event has occurred, a notification is sent.
The signal handler will receive the signal’s name and the signal name is uppercase based on the name of each event (e.g. ‘SIGTERM ‘ for SIGTERM  signals).
Syntax:
process.on(signalName, callback);
Parameters: This function accepts the following parameters:
signalName: It is the signal name that we want to call signals for.
callback: It is the callback function that is executed.
Example 1: SIGINT event
It is supported on all platforms via terminal, It is usually be generated with Ctrl+C (You can change this as well). It is not generated when the terminal raw mode is enabled and Ctrl+C is used. Here, the file name is index.js.
Javascript
process.stdin.resume(); // If SIGINT process is called then this will// run first then the next lineprocess.on('SIGINT', () => {  console.log('Hey Boss I just Received SIGINT.');}); // We are using this single function to handle multiple signalsfunction handle(signal) {  console.log(`So the signal which I have Received is: ${signal}`);} process.on('SIGINT', handle);
Run the index.js file using the following command:
node index.js
Output: On pressing ctrl + c key, the following will be the output on the console screen:
Hey Boss I just Received SIGINT.
So the signal which I have Received is: SIGINT
Example 2: SIGBREAK event
Here, the file name is index.js.
Javascript
// Requiring moduleconst express = require('express') const exp = express() exp.get('/', (req, res) => { res.send('Hello Sir')}) const server = exp.listen(3000, () => console.log('Server ready')) // Is delivered on Windows when Ctrl+Break is pressed.// On non-Windows platforms, it can be listened on, but there// is no way to send or generate it.process.on('SIGBREAK', () => { server.close(() => {   console.log('SIGNAL BREAK RECEIVED') })})
Run the index.js file using the following command:
node index.js
Output: On pressing ctrl + break to generate the signal key:
Server ready
SIGNAL BREAK RECEIVED
Note: You can try SIGKILL signal that tells a process to immediately shut down and would work as a process.exit(). You can use this as syntax process.kill(process.pid, ‘SIGTERM’)
SIGTERM: The SIGTERM signal is sent to a process of Node.js to request its termination. It is different from the SIGKILL signal, it can be listened to or ignored by the process. This allows the process to be shut down nicely, by releasing the allocated resources to it, like database connections or file handles. This way of shutting down applications is called graceful shutdown.
Reference: https://nodejs.org/api/process.html#process_signal_events
sagar0719kumar
ruhelaa48
NodeJS-Process
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
How to connect Node.js with React.js ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Remove elements from a JavaScript Array