-
-
Save s4y/1215700 to your computer and use it in GitHub Desktop.
var child_process = require('child_process'); | |
// exec: spawns a shell. | |
child_process.exec('ls -lah /tmp', function(error, stdout, stderr){ | |
console.log(stdout); | |
}); | |
// execFile: executes a file with the specified arguments | |
child_process.execFile('ls', ['-lah', '/tmp'], function(error, stdout, stderr){ | |
console.log(stdout); | |
}); |
This made things really clear. Thank you.
This was helpful. Thanks!
Can you give an example of running a python script with execFile? I have tried using 'python myProgram.py' 'myProgram.py' and './myProgram.py' but I get an ENOENT error every time.
you can child_process.execFile('pwd',callback)
, check the directory, I guess it's not where you running your command
@scarrick68 have you tried?
child_process.execFile('python', ['./myProgram.py'], (err, stdout, stderr) => {
if (err) throw err;
console.log(stdout, stderr);
});
What is this curl command with execFile
?
curl -X GET -u admin:root -H 'Content-Type: application/json' http://localhost:8080
How can I call the execFile method to run a bash script that returns a list of values?
My bash file (test.sh) returns a list of 8 values but the below script is printing only the last row of the output.
const { execFile } = require('child_process');
const child = execFile('./test.sh', (error,stdout,stderror) => {
if (error) {
throw error;
}
console.log(stdout);
});
Does it differenciate this 2 kind of errors:
- child_process didn't find your binary or the process broke;
- a "normal" error code was returned by your binary after execution
How can I execute a js file which is written in es6? I am trying to do something like below:
child_process.execFile('node', ['./script.js'], function(error, stdout, stderr){ console.log(stdout); });
But getting syntax error
how can i open explorer with specific dir with execFile method?
nice example, it really help me out. Could you give one on spawn, and fork? That will be corresponding with this exec and execFile example's arguments?
And what do you mean by "spawn a shell."?