Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js child process sudo

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

like image 584
heroZero Avatar asked Oct 11 '25 17:10

heroZero


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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!