Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
});
@L2L2L

This comment has been minimized.

Copy link

L2L2L commented Aug 23, 2014

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."?

@curcuz

This comment has been minimized.

Copy link

curcuz commented Dec 4, 2014

@L2L2L

child_process.exec spawns a new subshell every time it's invoked, child_process.execFile doesn't. You can read more here

@afonsomatos

This comment has been minimized.

Copy link

afonsomatos commented Jun 23, 2015

This made things really clear. Thank you.

@owntheweb

This comment has been minimized.

Copy link

owntheweb commented Jan 11, 2016

This was helpful. Thanks!

@scarrick68

This comment has been minimized.

Copy link

scarrick68 commented Jan 16, 2016

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

mykhailo-riabokon commented Mar 11, 2016

@scarrick68 have you tried?

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

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

This comment has been minimized.

Copy link

saeidalidadi commented Jul 30, 2016

What is this curl command with execFile ?

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

This comment has been minimized.

Copy link

ArenaHernandez commented Sep 24, 2018

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

This comment has been minimized.

Copy link

MappingSuite commented Nov 23, 2018

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.