Open In App

Node.js Process Signal Events

Improve
Improve
Like Article
Like
Save
Share
Report

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 line
process.on('SIGINT', () => {
  console.log('Hey Boss I just Received SIGINT.');
});
 
// We are using this single function to handle multiple signals
function 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 module
const 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



Last Updated : 25 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads