Skip to content

Instantly share code, notes, and snippets.

@pghalliday
Created November 18, 2012 19:16
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 pghalliday/4106966 to your computer and use it in GitHub Desktop.
Save pghalliday/4106966 to your computer and use it in GitHub Desktop.
Killing process trees
var http = require('http');
var PORT = 8080;
var server = http.createServer(function(request, response) {
response.end();
});
server.listen(PORT, function() {
console.log('Listening on port ' + PORT);
});
var spawn = require('child_process').spawn;
var child = spawn('node', ['./child.js'], {
stdio: 'inherit'
});
function killChild() {
console.log(this);
if (child) {
child.kill();
child = null;
}
}
process.on('exit', killChild.bind('exit'));
process.on('SIGTERM', killChild.bind('SIGTERM'));
process.on('SIGINT', killChild.bind('SIGINT'));
var spawn = require('child_process').spawn;
var child = spawn(
'node', [
'./parent.js'
], {
stdio: 'inherit'
}
);
setTimeout(function() {
console.log('All children must die');
child.kill();
}, 2000);
pghalliday@pghalliday-VirtualBox:~/Development/Spikes/NodeChildProcess$ node test.js
Listening on port 8080
All children must die
{ '0': 'S',
'1': 'I',
'2': 'G',
'3': 'T',
'4': 'E',
'5': 'R',
'6': 'M' }
{ '0': 'e', '1': 'x', '2': 'i', '3': 't' }
pghalliday@pghalliday-VirtualBox:~/Development/Spikes/NodeChildProcess$
********************************
At this point all processes have ended
C:\Users\pghalliday\Documents\GitHub\ChildKiller\test\Spikes [master +8 ~1 -0 !]> node .\test.js
Listening on port 8080
All children must die
C:\Users\pghalliday\Documents\GitHub\ChildKiller\test\Spikes [master +8 ~1 -0 !]>
*****************
At this point task manager shows that the child.js process is still running
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment