Node.js子进程sudo

时间:2017-11-15 19:26:43

标签: sudo child-process

我正在尝试使用涉及使用sudo的子进程。

终端可以正常使用:

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

然而,当我尝试将其作为子进程时:

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

我收到以下错误:

/home/pi/Desktop/fm_transmitter/execFile.js:71     抛出错误;     ^

错误:spawn sudo / home / pi / Desktop / fm_transmitter / bin / Release / fm_transmitter ENOENT

如何将sudo纳入子进程?

由于

1 个答案:

答案 0 :(得分:1)

基于execFile documentationfile参数将简单地为sudo,命令中的所有其他内容将为args参数。因此,对于您的示例,它看起来像这样:

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