10

I want to reboot/shutdown my linux OS from a node.js script. Only on things i can find are methods to stop a express server or a running function inside a script but not a way to shutdown/reboot my hole linux.

is there any way to do that?

Paules
  • 540
  • 1
  • 3
  • 13

5 Answers5

11

The nodejs command is:

require('child_process').exec('sudo /sbin/shutdown -r now', function (msg) { console.log(msg) });

To avoid running nodejs as su you must give permission to run this command. Give permissions by creating a file under the /etc/sudoers.d/ directory, e.g.

$ sudo nano /etc/sudoers.d/mysudoersfile

Add the following lines and change pi in the below snippet to the user nodejs will be running as:

pi ALL=/sbin/shutdown
pi ALL=NOPASSWD: /sbin/shutdown

This technique can also be applied to other commands. For example if you want to allow the user (in turn nodejs) to restart networking then add the following to your sudoers file.

pi ALL=/etc/init.d/networking
pi ALL=NOPASSWD: /etc/init.d/networking
Simon Hutchison
  • 2,949
  • 1
  • 33
  • 32
  • If you are wondering how to run nodejs as a specific user check this out: https://stackoverflow.com/questions/13385029/automatically-start-forever-node-on-system-restart – Simon Hutchison Apr 22 '18 at 23:44
4

thanks for so far. I got a solution thats working with that now. only it has no response that the command has been executed. For the rest it's working.

code:

console.log('loaded.....');
var exec = require('child_process').exec;

function execute(command, callback){
    exec(command, function(error, stdout, stderr){ callback(stdout); });
}



execute('shutdown -r now', function(callback){
    console.log(callback);
});
Paules
  • 540
  • 1
  • 3
  • 13
  • 1
    Likely, your Node.js server is executed by root, isn't it? It's a kind of security hole :) (Actually, it's not a hole but a potential threat that an intruder automatically gains access to your host as a superuser and you may never notice this intrusion). That's why I mentioned `sudo`. – user3159253 Apr 13 '14 at 03:17
  • Yes it's running in root but i don't know how to change it because it's a [BeagleBoard](http://beagleboard.org/Products/BeagleBone%20Black) with has a pre installed OS with everything in it. It has a auto start nodejs. i use cloud9 to program it. There is nothing els on the system so it's not a big issue. – Paules Apr 13 '14 at 07:41
0

You should use child_process and a command like sudo /sbin/reboot. Also you need to configure sudo to allow node.js user run /sbin/reboot w/o a password prompt. see /etc/sudoers.

user3159253
  • 16,836
  • 3
  • 30
  • 56
0

Reboot package does exactly what you are asking for, and be found here:

https://www.npmjs.com/package/reboot

Oren Pinsky
  • 419
  • 3
  • 19
0

I use the Paules reply and the reboot npm package docs, and now its working as expected.

Permissions (Linux only)

If you are to run node process under non-superuser, be sure to give node permissions to reboot the system:

sudo setcap CAP_SYS_BOOT=+ep usr/local/bin/node 
Tyler2P
  • 2,324
  • 26
  • 22
  • 31