Skip to content

Commit

Permalink
process: Send signal name to signal handlers
Browse files Browse the repository at this point in the history
PR-URL: #15606
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Bartosz Sosnowski <[email protected]>
  • Loading branch information
robertrossmann authored and MylesBorins committed Dec 12, 2017
1 parent 9a9aa88 commit 8d90db5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
11 changes: 11 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ Signal events will be emitted when the Node.js process receives a signal. Please
refer to signal(7) for a listing of standard POSIX signal names such as
`SIGINT`, `SIGHUP`, etc.

The signal handler will receive the signal's name (`'SIGINT'`,
`'SIGTERM'`, etc.) as the first argument.

The name of each event will be the uppercase common name for the signal (e.g.
`'SIGINT'` for `SIGINT` signals).

Expand All @@ -362,6 +365,14 @@ process.stdin.resume();
process.on('SIGINT', () => {
console.log('Received SIGINT. Press Control-D to exit.');
});

// Using a single function to handle multiple signals
function handle(signal) {
console.log(`Received ${signal}`);
}

process.on('SIGINT', handle);
process.on('SIGTERM', handle);
```

*Note*: An easy way to send the `SIGINT` signal is with `<Ctrl>-C` in most
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function setupSignalHandlers() {

wrap.unref();

wrap.onsignal = function() { process.emit(type); };
wrap.onsignal = function() { process.emit(type, type); };

const signum = constants[type];
const err = wrap.start(signum);
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-signal-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const assert = require('assert');

if (common.isWindows) {
common.skip('Sending signals with process.kill is not supported on Windows');
}

process.once('SIGINT', common.mustCall((signal) => {
assert.strictEqual(signal, 'SIGINT');
}));

process.kill(process.pid, 'SIGINT');

process.once('SIGTERM', common.mustCall((signal) => {
assert.strictEqual(signal, 'SIGTERM');
}));

process.kill(process.pid, 'SIGTERM');

// Prevent Node.js from exiting due to empty event loop before signal handlers
// are fired
setImmediate(() => {});

0 comments on commit 8d90db5

Please sign in to comment.