I am performing an image magick identify command via nodejs child_process.exec. and using the string returned from stdout in my script.
Everything works fine but the call prints the stdout msg on console, if the server is not restarted and the console is not cleared for some time, the console becomes messy with stdout messages.
Relevant Code :
var exec = require('child_process').exec;
exec('identify -verbose '+originalFilePath,function(err,stdout,stderr){
var strOut = stdout; // Do something with stdout
});
I just want to disable the printing of the returned result on the console.
In your exact situation, my best solution was to set stdio to 'pipe'. This gives exactly what you'd like.
const execSync = require('child_process').execSync;
try {
let options = {stdio : 'pipe' };
let stdout = execSync('echo hello' , options);
console.log("I got success: " + stdout);
execSync('rmdir doesntexist' , options);//will exit failure and give stderr
} catch (e) {
console.error("I got error: " + e.stderr ) ;
}
Result:
I got success: hello
I got error: rmdir: doesntexist: No such file or directory
Notice: Child process is silent
Nothing was printed to console by the child processes themselves, yet we get full stdout and stderr messages to work with.
This is at odds with the documentation* which states that pipe is the default configuration. In reality, setting stdio to pipe changes the behaviour.
* https://nodejs.org/api/child_process.html#child_process_options_stdio
Try this:
var exec = require('child_process').exec;
exec('identify -verbose '+originalFilePath, { stdio: ['pipe', 'pipe', 'ignore']}, function(err,stdout,stderr){
var strOut = stdout; // Do something with stdout
});
This ignores the stderr from the exec command but still displays errors and the normal output. See the doc for further configurations.
execSync('echo "hello"', { stdio: [] });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With