1

Goal

I want to show graphical password prompt in nodejs to elevate priviledge thus gain some power to copy file content into another, but the last is owned by root.

In the implementation, I try to execute dd along with it's argument with gksudo with exec() function.

exec = require('child_process').exec

printall = function (error, stdout, stderr) {
  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);
  if (error) {
    console.log('exec err: ' + error);
  }
}

exec("gksudo dd if=/home/user/minor.txt of=/home/user/major.txt", printall)

Error

But I always fail, with no good reason.

It said,

stdout: 
stderr: 
exec err: Error: Command failed: /bin/sh -c gksudo dd if=/home/user/minor.txt of=/home/user/major.txt

If I reproduce the command into terminal, it missed double quotes and instead run gksudo only. Well, in nodejs, it simply fails.

Notes

I originally develop Atom package. It's my first time, so, I found out about a different version of Node (or IOJs?). I execute the whole code inside Atom.

Question

If you expect a clear question, well, possibly

How to execute gksudo within node.js to run other command along with the arguments?

Abdillah
  • 982
  • 11
  • 28
  • You were talking about missing double quotes, did you try adding them with \"? – Florian Wendelborn Jul 13 '15 at 07:16
  • There also seems to be a npm module for this: https://www.npmjs.com/package/sudo – Florian Wendelborn Jul 13 '15 at 07:17
  • Thank you for your suggestion. Yes I've try adding it, but automatically `exec()` execute without `-c` option; leading to another error. The package sudo seems to work using console, as neither error nor dialog appear prompting for password. – Abdillah Jul 13 '15 at 07:52
  • What is the operating system? Did you try executing without the sudo? Did you try the [sync](https://nodejs.org/api/child_process.html#child_process_synchronous_process_creation) version? What about `fork()` or `spawn()?` – Justin Sep 02 '15 at 21:06

0 Answers0