Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When node.js goes down, how can I bring it back up automatically?

Tags:

node.js

Since node is basically a single process, when something goes terribly wrong, the whole application dies.

I now have a couple of apps built on express and I am using some manual methods to prevent extended downtimes ( process.on('uncaughtException') and a custom heartbeat monitor ).

Any suggestions from the community? Best-practices? Frameworks?

Thanks! A

like image 810
Aldo Bucchi Avatar asked Apr 14 '11 18:04

Aldo Bucchi


1 Answers

Use something like forever

or use supervisor.

Just npm link and then sudo supervisor server.js.

These types of libraries also support hot reloading. There are some which you use from the command line and they run node services as sub processes for you. There are others which expect you to write your code to reload itself.

Ideally what you want to move towards a full blown load balancer which is failure safe. If a single node proccess in your load balancer crashes you want it to be quietly restarted and all the connections and data rescued.

Personally I would recommend supervisor for development (It's written by isaacs!) and a full blown load balancer (either nginx or node) for your real production server.

Of course your already running multiple node server processes in parallel because you care about scaling across multiple cores right ;)

like image 175
Raynos Avatar answered Nov 15 '22 20:11

Raynos