Search code examples
linuxnode.jssocket.iorestart

Auto restart or Proper Handling socket.io server on exception in linux


I'ved got my socket.io server running in the background. However, there are several situation where it died. What can I do to make my socket.io server auto restart whenever it dies or terminated.

This is how I started my node server

> node server &

however the server thrown an exception and died.

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: Redis connection to pub-redis-us-east1-garantiabata.com:12087 failed - connect ETIMEDOUT 54.179.28.165:12087
    at RedisClient.on_error (/home/org-chatserver/node_modules/redis/index.js:185:24)
    at Socket.<anonymous> (/home/org-chatserver/node_modules/redis/index.js:95:14)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1253:8)
    at doNTCallback2 (node.js:450:9)
    at process._tickCallback (node.js:364:17)
^C

This is my environment

Linux version 2.6.32-042stab090.5 (root@kbuild-rh6-x64) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) ) #1 SMP Sat Jun 21 00:15:09 MSK 2014

and I believe my team is using this:

https://github.com/jbavari/ionic-socket.io-redis-chat

Solution

  • You can use pm2 to monitor logs, auto restart... etc. For example:

    sudo npm install -g pm2
    pm2 start app.js --watch
    

    More about watch & restart.