Nodejs PM2 and Forever
What is forever?
forever is a node.js package that is used to keep the server alive even when the server crash/stops. When the node server is stopped because of some error, exception, etc. forever automatically restarts it.
The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly and it will error log also.
From the npmjs https://www.npmjs.com/package/forever
A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)
Forever can be used as
forever start app.js
It provides many useful functions which you can see in the link above.
Install forever: npm install forever
Install forever globally: sudo npm install forever -g
For forever more commands: https://github.com/foreverjs/forever/
What is PM2?
PM2 has a rich set of functionalities and we just pointed out some of the features. There are a lot other process managers for Node.js applications like supervisor, forever, etc. We think PM2 has some major advantages over all others: intuitive API accessible via CLI, stability, great console output. That later may not be one of the critical requirements when deciding for a tool to use. Nevertheless, we had multiple situations where PM2’s output saved us a lot headaches and time when searching for process information
The list below outlines functionality which we think is highly beneficial for every system administrator running Node.js apps in production.
Major Feature Set
- Process management including automatic app restarts on failure or system reboots
- Application monitoring
- Declarative configuration via JSON file
- Log management
- Built-in cluster mode
- Startup script generation for *nix systems
- Seamless updates
- Integrated module system
To run PM2: pm2 start app.js
Install forever: npm install pm2
Install forever globally: sudo npm install pm2 -g
For PM2 more commands: https://github.com/tes/PM2
Compare pm2 and Forever
NPM vs YARN
What is npm?
What is yarn?
Yarn was developed by Facebook in attempt to resolve some of npm’s shortcomings. Yarn isn’t technically a replacement for npm since it relies on modules from the npm registry. Think of Yarn as a new installer that still relies upon the same npm structure. The registry itself hasn’t changed, but the installation method is different. Since Yarn gives you access to the same packages as npm, moving from npm to Yarn doesn’t require you to make any changes to your workflow.
To install npm in ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04
To install yarn in ubuntu: https://yarnpkg.com/lang/en/docs/install/#debian-stable
Common NPM commands in Yarn
Start an app-link
$ pm2 start app.js
Start and auto-restart on file change-link
$ pm2 start app.js –watch [–ignore-watch /*/]
Show extended info-link
$ pm2 show <app_name>
$ pm2 restart app
Restart and update env-link
$ NODE_ENV=production pm2 restart app –update-env
$ pm2 stop app
$ pm2 delete app
Show logs link
$ pm2 logs
Show env link
$ pm2 env <pm_id>
Start PM2 at boot-link
$ pm2 startup
Set a name-link
$ pm2 start app.js –name=”name”
# or update name
$ pm2 restart app –name=”new-name”
Reset Restart Counters link
$ pm2 reset all
$ pm2 monitor
Dump all process data-link
$ pm2 prettylist
$ pm2 show <pm_id|app_name>