File tree 3 files changed +36
-1
lines changed
3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -349,6 +349,9 @@ Signal events will be emitted when the Node.js process receives a signal. Please
349
349
refer to signal(7) for a listing of standard POSIX signal names such as
350
350
` SIGINT ` , ` SIGHUP ` , etc.
351
351
352
+ The signal handler will receive the signal's name (` 'SIGINT' ` ,
353
+ ` 'SIGTERM' ` , etc.) as the first argument.
354
+
352
355
The name of each event will be the uppercase common name for the signal (e.g.
353
356
` 'SIGINT' ` for ` SIGINT ` signals).
354
357
@@ -361,6 +364,14 @@ process.stdin.resume();
361
364
process .on (' SIGINT' , () => {
362
365
console .log (' Received SIGINT. Press Control-D to exit.' );
363
366
});
367
+
368
+ // Using a single function to handle multiple signals
369
+ function handle (signal ) {
370
+ console .log (` Received ${ signal} ` );
371
+ }
372
+
373
+ process .on (' SIGINT' , handle);
374
+ process .on (' SIGTERM' , handle);
364
375
```
365
376
366
377
* ` SIGUSR1 ` is reserved by Node.js to start the [ debugger] [ ] . It's possible to
Original file line number Diff line number Diff line change @@ -194,7 +194,7 @@ function setupSignalHandlers() {
194
194
195
195
wrap . unref ( ) ;
196
196
197
- wrap . onsignal = function ( ) { process . emit ( type ) ; } ;
197
+ wrap . onsignal = function ( ) { process . emit ( type , type ) ; } ;
198
198
199
199
const signum = constants [ type ] ;
200
200
const err = wrap . start ( signum ) ;
Original file line number Diff line number Diff line change
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 ( ( ) => { } ) ;
You can’t perform that action at this time.
0 commit comments