简体   繁体   中英

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? [Y/N] . But when using NodeJS execute command that time not ask anything and req. 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

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. 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

node-exec is meant for executing atomic commands that gets over in one shot. For interactive commands, you need to get access to a shell. Try this - 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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