Created
November 18, 2012 19:16
-
-
Save pghalliday/4106966 to your computer and use it in GitHub Desktop.
Killing process trees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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