Long time I’ve searched for a NodeJS process manager that keep our script alive forever, ensure that our service and the worker restarted when the server restarted. I mean in a seamless and beautiful way.
So finally the team from keymetrics.io (aFull Stack monitoring and App management web service) comes to the rescue. There highly active project PM2 [P(rocess) M(anager) 2] combine these cool features and is super easy to use.
The first, start a nodejs script
$ pm2 start app.js
The process is put in background, kept alive forever and monitored.
The second, managing the processes is pretty straightforward
$ pm2 list
$ pm2 stop <app_name|id|all> $ pm2 restart <app_name|id|all> $ pm2 delete <app_name|id|all>
The third, PM2 can generate and configure a startup script to keep PM2 and your processes alive at every server restart!
$ pm2 startup # auto-detect platform
(Execute the startup command only as the user to be running the PM2 daemon)
PM2 can also enables a built-in load balancing between multiple application to use all CPUs available in a server. Each HTTP/TCP/UDP request will be forwarded to one specific process at a time.
pm2 start app.js -i max # Enable load-balancer and cluster features
PM2 is amazing when we have to manage several applications at once. It allows you to declare the behaviour of each application within a simple JSON file.
This all is super smart.