Created
April 8, 2014 07:25
-
-
Save jameswomack/10099364 to your computer and use it in GitHub Desktop.
detachable command
This file contains 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
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