diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 6c8861b3b2e192..070596daf00afc 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1,4 +1,4 @@ -# Child Process +#n Child Process > Stability: 2 - Stable @@ -821,10 +821,11 @@ within the child process to close the IPC channel as well. added: v0.1.90 --> -* `signal` {String} +* `signal` {String|Number} The signal to send, either as a string or number. + Defaults to `'SIGTERM'`. -The `child.kill()` methods sends a signal to the child process. If no argument -is given, the process will be sent the `'SIGTERM'` signal. See signal(7) for +The `child.kill()` method sends a signal to the child process. If no argument +is given, the process will be sent the `'SIGTERM'` signal. See `signal(7)` for a list of available signals. ```js diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 5bdc53fd192073..3ea87206f5907b 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -363,8 +363,8 @@ function onErrorNT(self, err) { ChildProcess.prototype.kill = function(sig) { var signal; - if (sig === 0) { - signal = 0; + if (typeof sig === 'number' && sig >>> 0 === sig) { + signal = sig; } else if (!sig) { signal = constants['SIGTERM']; } else { diff --git a/test/parallel/test-child-process-kill.js b/test/parallel/test-child-process-kill.js index 9f6f8f6e796196..aea87cae1f1d26 100644 --- a/test/parallel/test-child-process-kill.js +++ b/test/parallel/test-child-process-kill.js @@ -1,18 +1,25 @@ 'use strict'; -var common = require('../common'); -var assert = require('assert'); -var spawn = require('child_process').spawn; -var cat = spawn(common.isWindows ? 'cmd' : 'cat'); +const common = require('../common'); +const assert = require('assert'); +const spawn = require('child_process').spawn; +const constants = process.binding('constants'); -cat.stdout.on('end', common.mustCall(function() {})); -cat.stderr.on('data', common.fail); -cat.stderr.on('end', common.mustCall(function() {})); +function runTest(sig) { + const cat = spawn(common.isWindows ? 'cmd' : 'cat'); -cat.on('exit', common.mustCall(function(code, signal) { - assert.strictEqual(code, null); - assert.strictEqual(signal, 'SIGTERM'); -})); + cat.stdout.on('end', common.mustCall(function() {})); + cat.stderr.on('data', common.fail); + cat.stderr.on('end', common.mustCall(function() {})); -assert.equal(cat.killed, false); -cat.kill(); -assert.equal(cat.killed, true); + cat.on('exit', common.mustCall(function(code, signal) { + assert.strictEqual(code, null); + assert.strictEqual(signal, 'SIGTERM'); + })); + + assert.equal(cat.killed, false); + cat.kill(); + assert.equal(cat.killed, true); +} + +runTest(constants.os.signals.SIGTERM); +runTest('SIGTERM');