is there a way to pass extra arguments to the callback function when i use
child_process.exec(cmd,callback)
?
According to the documentation, the callback function only receive error,stdout,sterr.
I could eventually have an unix script who gets extra args, runs the command, and outputs result of the command and args to stdout but maybe there is a better way to do this
Thanks
The first argument of the callback handler should be the error and the second argument can be the result of the operation. While calling the callback function if there is an error we can call it like callback(err) otherwise we can call it like callback(null, result).
The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() .
exec() method: This method runs a command in a console and buffers the output. child_process. spawn() method: This method launches a new process with a given command. child_process. fork() method: This method is a special case of spawn() method to create child processes.
You can call another function inside the exec
callback
var exec = require('child_process').exec
function(data, callback) {
var cmd = 'ls'
exec(cmd, function (err, stdout, stderr) {
// call extraArgs with the "data" param and a callback as well
extraArgs(err, stdout, stderr, data, callback)
})
}
function extraArgs(err, stdout, stderr, data, callback) {
// do something interesting
}
At the end, i have a function my_exec :
var exec = require('child_process').exec
function my_exec(cmd,data,callback)
{
exec(cmd,function(err,stdout,stderr){
callback(err,stdout,stderr,data)
})
}
thank you!
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