Tutorial

Getting Started with PM2, the Node.js Process Manager

Published on August 1, 2019
author

Cooper Makhijani

Getting Started with PM2, the Node.js Process Manager

PM2, or Process Manager 2 is an incredibly versatile production process manager written in Node.js.

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.

Restarting on File Changes

Another awesome feature that PM2 has is restarting when a file in the working directory changes. To make PM2 watch our directory and restart on file changes, include the --watch flag when you start your app.

Persistence Through Restarts

PM2 also allows us to start our processes when our server (re)starts. Start whatever you want to have running all the time through pm2 start and then run pm2 save.

$ pm2 save                                                                                                
[PM2] Saving current process list...
[PM2] Successfully saved in C:\Users\moose\.pm2\dump.pm2

Now, when our system restarts, PM2 will start whatever processes we had running when we ran pm2 save.

🐊 PM2 does a lot of things, most of which are out of the scope of this brief introduction. In the future we might cover PM2’s cluster mode, remote process management and more. Thanks for reading!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Cooper Makhijani

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.