参见文档
here.它非常具体如何传递命令行参数。注意,你可以使用exec或spawn。 spawn有一个特定的参数为命令行参数,而使用exec你只是传递参数作为命令字符串的一部分来执行。
直接从文档,与解释评论内联
var util = require('util'),spawn = require('child_process').spawn,ls = spawn('ls',['-lh','/usr']); // the second arg is the command
// options
ls.stdout.on('data',function (data) { // register one or more handlers
console.log('stdout: ' + data);
});
ls.stderr.on('data',function (data) {
console.log('stderr: ' + data);
});
ls.on('exit',function (code) {
console.log('child process exited with code ' + code);
});
而与exec
var util = require('util'),exec = require('child_process').exec,child;
child = exec('cat *.js bad_file | wc -l',// command line argument directly in string
function (error,stdout,stderr) { // one easy function to capture data/errors
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
最后,注意exec缓冲输出。如果你想将输出流回客户端,你应该使用spawn。