Skip to content

Instantly share code, notes, and snippets.

@letalumil
Forked from kevinohara80/child.js
Created August 31, 2016 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letalumil/941b0c86e3c36f8ae4a96c8e7b1a6f50 to your computer and use it in GitHub Desktop.
Save letalumil/941b0c86e3c36f8ae4a96c8e7b1a6f50 to your computer and use it in GitHub Desktop.
killing a node.js child process with infinite loop
while(true) {
console.log('blah');
}
// to execute this test run `node parent.js`
var spawn = require('child_process').spawn;
console.log('Spawning child process');
var child = spawn('node', ['child.js']);
child.on('exit', function(){
clearTimeout(to);
console.log('Child exited!');
});
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
var to = setTimeout(function(){
console.log('Sending sigkill');
child.kill();
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment