How to use the execFile function from child_process

Find comprehensive JavaScript child_process.execFile code examples handpicked from public code repositorys.

The child_process.execFile method in Node.js is used to asynchronously spawn a shell command in a new process and execute it.

3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
}


Brain.prototype._powerOff = function _powerOff () {
  this._setState('powerOff')
  console.log('powering off')
  cp.execFile('poweroff', ['-d', '2'], {}, function () {
    process.exit(0)
  })
}

fork icon129
star icon95
watch icon17

+ 3 other calls in file

339
340
341
342
343
344
345
346
347
348
  return res.status(404).send("Error: Route protected")
}


res.send('done');
execFile('bash', ['/home/revo/nodeutils', '-forcereboot'], (err, stdout, stderr) => {
  if (err) {
    res.status(404).send(err);
  } else {
    (stdout);
fork icon0
star icon1
watch icon1

+ 84 other calls in file

How does child_process.execFile work?

The child_process.execFile() method is used to asynchronously run an executable file and returns a buffer with the stdout and stderr output of the command. It takes the path of the executable as the first argument, and an array of string arguments as the second argument, along with optional options such as environment variables and working directory.

1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
if(userIsCreated && !authResult){
  return res.status(404).send("Error: Route protected")
}


  execFile('bash', ['/home/revo/nodeutils', '-clearbanned'], (err, stdout, stderr) => {
    if (err) {
      res.send("Error: Node already added");
    } else {
      res.send("ok");        
fork icon0
star icon1
watch icon1

+ 19 other calls in file

62
63
64
65
66
67
68
69
70
71
72
      return '{}'
  }
}


Chart.prototype.screenshot = function(savePath, callback) {
  child_process.execFile(screenshot, [
    this.url(),
    savePath,
    this.width,
    this.height
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const { execFile } = require("child_process");

const directoryPath = "/usr";
const options = { maxBuffer: 1024 * 1024 };

execFile("ls", [directoryPath], options, (error, stdout, stderr) => {
  if (error) {
    console.error(`execFile error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
});

In this example, we're executing the ls command on the /usr directory and passing in a maxBuffer option to limit the amount of data that can be returned in stdout. The callback function receives the command's output (if any) as the stdout parameter.