How do i reboot linux from nodejs

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?

  • Node.js source code build giving segmentation fault on ARM
  • Which hogan.js templeting package to use with express.js?
  • How to get req and res object of Expreejs in .ejs file
  • HBase: Thrift vs Rest performance
  • Search error while using Datatable with huge data in NodeJS
  • Can't get stylesheet to work with ejs for node.js
  • Is there a bettery way to work with nested(associated) models in Sails JS?
  • Interpreting JavaScript in PHP
  • How to pipe one readable stream into two writable streams at once in Node.js?
  • How to register a url protocol handler in Node.js
  • javascript promise not passing all arguments (using Q)
  • trying to use node.js and mysql to connect to different database per user - preferably using a pool
  • 4 Solutions collect form web for “How do i reboot linux from nodejs”

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

    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 /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
    

    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.

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

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

    Node.js is the Best Javascript runtime in the world.