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);
});
@afonsomatos
Copy link

This made things really clear. Thank you.

@owntheweb-archive
Copy link

This was helpful. Thanks!

@scarrick68
Copy link

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.

@eatgrass
Copy link

eatgrass commented Feb 3, 2016

@scarrick68

you can child_process.execFile('pwd',callback), check the directory, I guess it's not where you running your command

@mykhailo-riabokon
Copy link

@scarrick68 have you tried?

child_process.execFile('python', ['./myProgram.py'], (err, stdout, stderr) => {
  if (err) throw err;

  console.log(stdout, stderr);
});

@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