0

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);
});
};

3 Answers 3

1

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")
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

cmd.stdin.write("Y"); Nver exec. because in function callback exec. before last row exec.
@GunjanPatel I copy-pasted your code and forgot to remove callback, maybe that confused you. But the cmd.stdin.write("Y") should be outside the exec block and be called after that function not in its callback.
This is of course assuming that exec halts and is waiting for input. Not sure what will happen if exec has returned by the time you try to write to stdin
0
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);

1 Comment

I dont have issue with that i have execute above command in linux that time if file available than ask me to overwite Y/N but if using NodeJS nothing happen execute command that time background what happen we dont know how can i give ans of that Que. [Y/n]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.