Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8d90db5

Browse filesBrowse files
robertrossmannMylesBorins
authored andcommitted
process: Send signal name to signal handlers
PR-URL: #15606 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
1 parent 9a9aa88 commit 8d90db5
Copy full SHA for 8d90db5

File tree

Expand file treeCollapse file tree

3 files changed

+36
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+36
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/process.md‎

Copy file name to clipboardExpand all lines: doc/api/process.md
+11Lines changed: 11 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ Signal events will be emitted when the Node.js process receives a signal. Please
350350
refer to signal(7) for a listing of standard POSIX signal names such as
351351
`SIGINT`, `SIGHUP`, etc.
352352

353+
The signal handler will receive the signal's name (`'SIGINT'`,
354+
`'SIGTERM'`, etc.) as the first argument.
355+
353356
The name of each event will be the uppercase common name for the signal (e.g.
354357
`'SIGINT'` for `SIGINT` signals).
355358

@@ -362,6 +365,14 @@ process.stdin.resume();
362365
process.on('SIGINT', () => {
363366
console.log('Received SIGINT. Press Control-D to exit.');
364367
});
368+
369+
// Using a single function to handle multiple signals
370+
function handle(signal) {
371+
console.log(`Received ${signal}`);
372+
}
373+
374+
process.on('SIGINT', handle);
375+
process.on('SIGTERM', handle);
365376
```
366377

367378
*Note*: An easy way to send the `SIGINT` signal is with `<Ctrl>-C` in most
Collapse file

‎lib/internal/process.js‎

Copy file name to clipboardExpand all lines: lib/internal/process.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function setupSignalHandlers() {
195195

196196
wrap.unref();
197197

198-
wrap.onsignal = function() { process.emit(type); };
198+
wrap.onsignal = function() { process.emit(type, type); };
199199

200200
const signum = constants[type];
201201
const err = wrap.start(signum);
Collapse file

‎test/parallel/test-signal-args.js‎

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
if (common.isWindows) {
7+
common.skip('Sending signals with process.kill is not supported on Windows');
8+
}
9+
10+
process.once('SIGINT', common.mustCall((signal) => {
11+
assert.strictEqual(signal, 'SIGINT');
12+
}));
13+
14+
process.kill(process.pid, 'SIGINT');
15+
16+
process.once('SIGTERM', common.mustCall((signal) => {
17+
assert.strictEqual(signal, 'SIGTERM');
18+
}));
19+
20+
process.kill(process.pid, 'SIGTERM');
21+
22+
// Prevent Node.js from exiting due to empty event loop before signal handlers
23+
// are fired
24+
setImmediate(() => {});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.