Skip to main content
se añadieron 384 caracteres en el cuerpo
Origen Enlace
jonilgz
  • 688
  • 4
  • 15

Se haría uso de exec.

Para apagar el 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);
});

Para reiniciar:

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Para evitar que se te soliciten las credenciales, deberás dar permisos al usuario de node.js para ejecutar dicha tarea:

Creamos un fichero en /etc/sudoers.d/ que le añadiremos el siguiente contenido:

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

Recuerda modificar USUARIODENODEJS por el usuario correspondiente.

Saludos,

Se haría uso de exec.

Para apagar el PC:

// shutdown.js

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

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

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

Para reiniciar:

// reboot.js

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

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

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Saludos,

Se haría uso de exec.

Para apagar el 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);
});

Para reiniciar:

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Para evitar que se te soliciten las credenciales, deberás dar permisos al usuario de node.js para ejecutar dicha tarea:

Creamos un fichero en /etc/sudoers.d/ que le añadiremos el siguiente contenido:

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

Recuerda modificar USUARIODENODEJS por el usuario correspondiente.

Saludos,

Se me fue la cabeza, puse nano en vez de sudo.
Origen Enlace
jonilgz
  • 688
  • 4
  • 15

Se haría uso de exec.

Para apagar el PC:

// shutdown.js

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

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

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

Para reiniciar:

// reboot.js

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

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

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Saludos,

Se haría uso de exec.

Para apagar el PC:

// shutdown.js

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

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

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

Para reiniciar:

// reboot.js

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

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

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Saludos,

Se haría uso de exec.

Para apagar el PC:

// shutdown.js

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

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

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

Para reiniciar:

// reboot.js

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

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

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Saludos,

Origen Enlace
jonilgz
  • 688
  • 4
  • 15

Se haría uso de exec.

Para apagar el PC:

// shutdown.js

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

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

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

Para reiniciar:

// reboot.js

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

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

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

Fuente: https://ourcodeworld.com/articles/read/411/how-to-shutdown-and-reboot-linux-with-node-js

Saludos,