Shutdown and restart operating system with javascript

0

How could I send restart and also turn off the pc with javascript in the frontend or with NodeJS in the backend ?, I use Linux as SO . I was researching and I read something about hyperlinking a file .lnk that would perform the action, but I have no idea how to make the file .lnk .

Thanks in advance ...

    
asked by Travv 30.11.2017 в 15:23
source

1 answer

2

Exec would be used.

To turn off the PC:

// shutdown.js

// Require child_process
var exec = require('child_process').exec;

// Create shutdown function
function shutdown(callback){
    exec('sudo /sbin/shutdown now', function(error, stdout, stderr){ callback(stdout); });
}

// Reboot computer
shutdown(function(output){
    console.log(output);
});

To restart:

// reboot.js

// Require child_process
var exec = require('child_process').exec;

// Create shutdown function
function shutdown(callback){
    exec('sudo /sbin/shutdown -r now', function(error, stdout, stderr){ callback(stdout); });
}

// Reboot computer
shutdown(function(output){
    console.log(output);
});
  

Source:    link

To avoid being asked for credentials, you must give the node.js user permission to execute the task:

We create a file in /etc/sudoers.d/ that we will add the following content:

USUARIODENODEJS ALL=/sbin/shutdown
USUARIODENODEJS ALL=NOPASSWD: /sbin/shutdown

Remember to modify USUARIODENODEJS by the corresponding user.

Greetings,

    
answered by 30.11.2017 / 15:32
source