-2

I have asimple 1 to 1 client and server nodejs based application. The nodejs is running on Linux(Intel Edison). What I am trying to make happen is for the Linux server to reboot automatically whenever the socket is broken. Is this possible?

This is my simple node-socketIO server

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
      console.log(msg)
    io.emit('chat message', msg);
  });

  socket.on('disconnect', function(){
      //how to programmatically reboot?
          console.log('user disconnected');
      });
});

http.listen(3000, function(){
  console.log('listening on *:3000 yeah');
});
0andriy
  • 3,641
  • 22
  • 29
Bachalo
  • 5,885
  • 22
  • 86
  • 175
  • 2
    Seriously, when a client disconnects, you want to reboot the server? That seems like a troublesome design concept. Clients should reconnect to the server on their own when they want to. You should only reboot the server if you've established that there's a non-recoverable problem with the server and, in most cases with that, you can just restart the server process, not reboot the whole server OS. – jfriend00 Mar 03 '15 at 01:30

1 Answers1

0

have you tried reboot(npm)

Install:

npm install reboot

Usage:

Call sync() and then reboot:

require('reboot').reboot();

Reboot without sync()ing:

require('reboot').rebootImmediately();

also setting permissions: Permissions

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
mido
  • 20,728
  • 10
  • 84
  • 109
  • 1
    While this may do what the OP asked for, I seriously question whether the OP is pursuing the right course of action in rebooting the entire server just because one client disconnected. That logic seems seriously flawed to me. – jfriend00 Mar 03 '15 at 01:31
  • @jfriend00 In high-availability scenarios you want to suicide or kill the other guy the nanosecond it loses touch with you, so when you take over you are sure you are not running in parallel, which may cause data corruption. – Tiberiu-Ionuț Stan Apr 10 '17 at 11:46