Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Created April 8, 2014 07:25
Show Gist options
  • Save jameswomack/10099364 to your computer and use it in GitHub Desktop.
Save jameswomack/10099364 to your computer and use it in GitHub Desktop.
detachable command
function command(cmd, shouldEnd, shouldDetach) {
var args = cmd.trim().split(' ');
var cmdName = args.shift();
var opts = shouldDetach ? {detached: true, stdio: ['ignore', out, err] } : {};
var child = require('child_process').spawn(cmdName, args, opts);
if (shouldDetach) {
console.log('Spawned child ' + cmdName + ' with pid ' + child.pid);
child.unref();
} else {
child.stdout.pipe(process.stdout, {end: shouldEnd});
child.stderr.pipe(process.stderr, {end: shouldEnd});
}
return child;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment