Skip to content

Instantly share code, notes, and snippets.

@liyu1981
Created March 26, 2014 01:53
Show Gist options
  • Select an option

  • Save liyu1981/9775524 to your computer and use it in GitHub Desktop.

Select an option

Save liyu1981/9775524 to your computer and use it in GitHub Desktop.
simple wrapper of nodejs native spawn child process for a exec like func
function runCmd(cmd, args, callback) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var out = '';
var err = '';
child.stdout.on('data', function(buffer) { out += buffer.toString(); });
child.stderr.on('data', function(buffer) { err += buffer.toString(); });
child.on('close', function(code) { callback(code, out, err); });
}
runCmd('ls', [ '/usr' ], function(code, stdout, stderr) {
if (code !== 0) {
console.error(stderr);
process.exit(code);
}
console.log(stdout);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment