8. PM2, the Node.js Process Manager

 

Uses for PM2



PM2 has a lot of uses, let’s look at a few:

  • Restarting after crashes: PM2 allows us to keep processes running until the heat death of the universe, or a server failure, whichever happens first
  • Monitoring and managing processes remotely: A magic-powered web portal allows you to keep an eye on remote processes and manage them
  • It doesn’t just run Node apps: PM2 isn’t limited to just Node.js processes, that’s right, you can even use it to keep your Minecraft server online
  • Restart-Persistance: PM2 can remember all your processes and restart them after a system restart
  • And a whole lot more

Getting started

First thing we need to do is to install PM2 globally on your machine:

$ npm i -g pm2

Basic Commands

Let’s get into the basics of how to use it. To start a process under PM2, all you have to do is run pm2 start <app>. App being the name of the file you’re running. PM2 will output something like this:

[PM2] Starting C:\Users\moose\app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬
│ App name │ id │
├──────────┼────|
│ app      │ 0  │
└──────────┴────|

Reduced and simplified for brevity

Pay attention to what it says under id. If you forget just run pm2 list. If you want to add a name to the process when you start it, you can use the --name parameter (pm2 start app.js --name mycoolapp). Awesome! Your app is now running under PM2, if it crashes, PM2 will restart it.

Comments

Popular posts from this blog

10. Debugging in Visual Studio Code

6. How to setup a local MongoDB Connection

11. Overview of Project Deployment Using Heroku