PM2 application manager
Use NPM To Install A Package Called PM2.
NPM is a package manager that you will use to install frameworks and libraries to use with your Node.js applications. NPM was installed with Node.js. PM2 is a sweet little tool that is going to solve two problems for you:
It is going to keep your site up by restarting the application if it crashes. These crashes should NOT happen, but it is good know that PM2 has your back. (Some people may be aware of Forever.js, another tool that is used to keep node based sites running - I think you will find that PM2 has a lot to offer.)
It is going to help you by restarting your node application as a service every time you restart the server. Some of use know of other ways to do this, but pm2 makes it easier, and it has some added flexibility.
Install PM2 by typing the following at the command line:
sudo npm install pm2 -g
From <https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production- environment-on-an-ubuntu-vps>
Pm2-logrotate
Install pm2-logrotate to manage pm2 logs:
https://www.npmjs.com/package/pm2-logrotate
To make changes to pm2-logrotate configuration file, you'll have to go to: /.pm2/module_config.json
Cheatsheet
# Fork mode
pm2 start app.js --namemy-api # Name process# Cluster mode
pm2 start app.js -i0 # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -imax # Same as above, but deprecated.
pm2 scale app +3 # Scales `app` up by 3 workers
pm2 scale app 2 # Scales `app` up or down to 2 workers total# Listing
pm2 list # Display all processes status
pm2 jlist # Print process list in raw JSON
pm2 prettylist # Print process list in beautified JSON
pm2 describe 0 # Display all informations about a specific process
pm2 monit # Monitor all processes# Logs
pm2 logs [--raw] # Display all processes logs in streaming
pm2 flush # Empty all log files
pm2 reloadLogs # Reload all logs# Actions
pm2 stop all # Stop all processes
pm2 restart all # Restart all processes
pm2 reload all # Will 0s downtime reload (for NETWORKED apps)
pm2 stop 0 # Stop specific process id
pm2 restart 0 # Restart specific process id
pm2 delete 0 # Will remove process from pm2 list
pm2 delete all # Will remove all processes from pm2 list# Misc
pm2 reset <process> # Reset meta data (restarted time...)
pm2 updatePM2 # Update in memory pm2
pm2 ping # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart
From <https://pm2.keymetrics.io/docs/usage/quick-start/>
Pm2 and memory usage
https://jsantacl.medium.com/pm2-and-memory-usage-in-node-js-apps-fd17394fcb40