Sudo Command Node Webkit

How can I execute a command in Node Webkit using sudo privileges? I am trying to change file permissions in Node and also create directories in places where sudo is required.

+3


source to share


1 answer


With nodejs, you can execute commands in a shell with a child process and an exec function. Here is the documentation: http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Example:

var exec = require('child_process').exec;
var child;

child = exec('sudo chown -R username:group /home/myuser/myfolder',
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

      



Now you have stdout, stdint, which are streams, and you can write your password by writing it to the stream like this:

child.stdin.write("mypassword");

      

You can also check out this module for using sudo commands: https://www.npmjs.org/package/sudo

0


source







All Articles