Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created January 24, 2012 03:21
Show Gist options
  • Select an option

  • Save cowboy/1667586 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/1667586 to your computer and use it in GitHub Desktop.
If a forked node process exits, how can the parent know?
console.log('child spawned');
process.on('exit', function() {
// This never executes.
process.send('exited from within child');
});
function tellParentImDone() {
process.send('hey parent, all done');
}
// WHOOPS BROKEN 3RD PARTY CODE FORGOT TO DO THIS
// tellParentImDone();
// Run with: node parent.js
var fork = require('child_process').fork;
var child = fork('child.js');
child.on('exit', allDone.bind(null, 'exit'));
child.on('message', allDone.bind(null, 'message'));
function allDone(mode, arg) {
console.log(mode + ':', arg);
child.kill();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment