简体   繁体   English

使用NodeJ执行Linux命令面临错误

[英]Using NodeJs execute Linux commands Face error

I used nodeJS for execute linux command but when i execute command than if "myWaveFile.wav" file generated already than ask can you overwite it? 我使用nodeJS来执行linux命令,但是当我执行命令时,如果已经生成了“ myWaveFile.wav”文件,那么请问是否可以覆盖它? [Y/N] . [是/否]。 But when using NodeJS execute command that time not ask anything and req. 但是,当使用NodeJS execute命令时,此时无需询问任何内容并要求。 failed after some time. 一段时间后失败。

var sys = require('sys');
var exec = require('child_process').exec;
var _cmd = "avconv -i /root/builds/SpeechRecognition/records/wave_file.wav  -acodec pcm_s16le -ar 16000 /root/builds/SpeechRecognition/records/myWaveFile.wav";


//ExecCMD function call from other files


exports.ExecCMD = function(_cmd, callback){
exec(_cmd, function (error, stdout, stderr){
    //sys.puts(stdout);
    callback(error, stdout, stderr);
});
};

As per the Node API docs: exec returns a ChildProcess 根据Node API文档: exec返回一个ChildProcess

So you should be able to do something like: 因此,您应该能够执行以下操作:

var cmd = exec(_cmd, function(error, stdout, stderr){
    console.log(stdout);
});

cmd.stdin.write("Y");

You could also try using spawn instead of exec. 您也可以尝试使用spawn而不是exec。 This gives you the ability to listen to stdout and make more informed decisions on what to write on stdin instead of guessing ahead of time 这使您能够聆听stdout并就在stdin上写的内容做出更明智的决定,而无需提前猜测

node-exec is meant for executing atomic commands that gets over in one shot. node-exec用于执行一次完成的原子命令。 For interactive commands, you need to get access to a shell. 对于交互式命令,您需要访问外壳程序。 Try this - https://github.com/arturadib/shelljs 试试这个-https://github.com/arturadib/shelljs

require('shelljs/global');
var _cmd = "avconv -i /root/builds/SpeechRecognition/records/wave_file.wav  -acodec pcm_s16le -ar 16000 /root/builds/SpeechRecognition/records/myWaveFile.wav";
echo("Y");
echo("\n")
exec(_cmd, function puts(error, stdout, stderr){
    //sys.puts(stdout);
    callback(error, stdout, stderr);
});

Why do you write function name??? 为什么写函数名???

Maybe you need anonymous function right??? 也许您需要匿名函数吧???

exec(_cmd, function(error, stdout, stderr){
    //sys.puts(stdout);
    callback(error, stdout, stderr);
});

And then you call only callback function with same arguments dont need anonymous function 然后您只调用具有相同参数的回调函数就不需要匿名函数

exec(_cmd, callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM