Created
March 26, 2014 01:53
-
-
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
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
| 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