Skip to content

Instantly share code, notes, and snippets.

@s4y
Created September 14, 2011 02:12
Show Gist options
  • Save s4y/1215700 to your computer and use it in GitHub Desktop.
Save s4y/1215700 to your computer and use it in GitHub Desktop.
child_process.execFile example
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);
});
@saeidalidadi
Copy link

What is this curl command with execFile ?

curl -X GET -u  admin:root -H 'Content-Type: application/json'  http://localhost:8080

@ArenaHernandez
Copy link

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

@MappingSuite
Copy link

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

@kanthvallampati
Copy link

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

@alighamdan
Copy link

how can i open explorer with specific dir with execFile method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment