Node.js child process sudo

Viewed 4221

I am trying to use a child process that involves using sudo.

It works fine in terminal:

  sudo /home/pi/Desktop/fm_transmitter/bin/Release/fm_transmitter high_dash.wav 103.50

However, when i try it as a child process:

const execFile = require('child_process').execFile;
const child =execFile('sudo /home/pi/Desktop/fm_transmitter/bin/Release/fm_transmitter', ['high_dash.wav 103.50'] ,(error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});
}

I get the following error:

/home/pi/Desktop/fm_transmitter/execFile.js:71 throw error; ^

Error: spawn sudo /home/pi/Desktop/fm_transmitter/bin/Release/fm_transmitter ENOENT

How can i incorporate sudo into a child process?

Thanks

1 Answers

Based on the execFile documentation, the file parameter will simply be sudo and everything else in the command will be the args parameter. So for your example it would look like this:

const execFile = require('child_process').execFile;
const child = execFile('sudo', ['/home/pi/Desktop/fm_transmitter/bin/Release/fm_transmitter', 'high_dash.wav', '103.50'], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});
Related