Knowledgebase

What is PM2 and How Can You Use it?

Have you ever faced challenges managing your Node.js application, especially keeping it running smoothly without interruptions?

That’s where PM2 comes in!

In the world of modern web applications, ensuring seamless performance and uninterrupted uptime is critical. For developers working with Node.js, managing processes manually can be daunting and error-prone.

PM2 is like a smart assistant for your Node.js apps—it helps you start, stop, monitor, and even restart your app if it crashes. Think of it as a tool that makes your life easier by handling the technical headaches while you focus on building awesome things.

In this article, we will explore what PM2 is, why it’s helpful, and how you can start using it step by step. Don’t worry; it’s simpler than it sounds! 

Let’s dive in and make your application management stress-free!

What is PM2?

PM2 is a popular process manager for Node.js applications. It simplifies managing, monitoring, and deploying Node.js applications in production environments. 

PM2 ensures that applications run continuously and helps manage multiple processes seamlessly, making it an essential tool for developers and DevOps teams.


Also Read: Install & Configure a CSF Firewall in a CenTOS Server


Key Features of PM2

Process Management:

  1. You can start, stop, and restart applications easily.
  1. You can easily manage multiple Node.js applications simultaneously.

Auto-Restart:

  1. It automatically restarts applications when they crash or experience an error.

Cluster Mode:

  1. It supports running applications in cluster mode for better scalability and performance by utilizing multiple CPU cores.

Monitoring:

  1. It offers detailed application metrics like CPU usage, memory consumption, and process uptime.

Log Management:

  1. It provides robust logging capabilities, allowing developers to view real-time logs or analyze logs for debugging purposes.

Load Balancing:

  1. It balances the application load across multiple instances.

Deployment Automation:

  1. It simplifies application deployment using its integrated deployment system.

Cross-Platform Support:

  1. It works on Linux, macOS, and Windows.

Also Read: How to Create a User in Linux & Add it to the sudoer File?


Why Should You Use PM2?

Think of PM2 as your app’s personal assistant. It runs your Node.js app smoothly, so you don’t have to stress about it. Here’s why you’ll love it:

  •  Keeps Your App Running: If your app crashes (which can happen), PM2 jumps in and restarts it automatically. There is no downtime, no worries.
  •  Handles Growth Like a Pro: Got more users coming in? PM2 can run multiple instances of your app and efficiently use all your CPU power. With this, your app stays fast even when things get busy.
  •  Easy Monitoring: Wondering how your app is doing? PM2 is like a health tracker for your app. It shows you everything—CPU usage, memory, and more—in one simple dashboard.
  •  Simplifies Debugging: Logs can be a lifesaver when something goes wrong. PM2 collects all the logs so you can find and fix issues quickly.
  •  Set It and Forget It: PM2 can start your app automatically when your server reboots. It’s like setting an alarm; you don’t have to remember to do it manually.

In short, PM2 is a 24/7 manager for your Node.js apps that makes your life easier by keeping your app stable, scalable, and easy to manage. 


Also Read: How to Install MySQL Server in Windows?


How Can You Use PM2?

Here, we are going to explain how you can use PM2 to manage things efficiently. 

We will start by installing PM2!

Installing PM2

PM2 is a Node.js package and can be installed using npm or yarn. To install it globally, use the following command:

npm install -g pm2@latest

Once installed, we will know the basic commands that you can use in it!

Basic PM2 Commands

Here are some basic commands to get started:

1. Start an Application

To start an application, use:

pm2 start app.js

2. View Running Processes

To list all running processes, use:

pm2 list

3. Stop an Application

To stop an application, use:

pm2 stop app.js

4. Restart an Application

To restart an application, use:

pm2 restart app.js

5. Monitor Applications

To monitor application performance and resource usage, use:

pm2 monit

6. View Logs

To view logs for an application, use:

pm2 logs

7. Save the Current State

To save the current state of processes so they restart automatically on system reboot, use:

pm2 save

8. Auto-Startup Configuration

To configure PM2 to restart processes automatically after a system reboot:

pm2 startup

Also Read: How to Check the Response Time of a Website?


Advanced PM2 Usage

1. Running Applications in Cluster Mode

PM2’s cluster mode enables better performance by using multiple CPU cores:

pm2 start app.js -i max

*Note: Here, -i max automatically spawns instances equal to the number of CPU cores available.

2. Configuring Applications with JSON

You can define application configurations in a process.json file:

{
  “apps”: [
    {
      “name”: “app1”,
      “script”: “./app.js”,
      “instances”: 2,
      “autorestart”: true,
      “watch”: false,
      “max_memory_restart”: “500M”
    }
  ]
}

Start applications using the configuration file:

pm2 start process.json

3. Deployment Automation:

PM2 simplifies deployments with its built-in deploy command:

pm2 deploy ecosystem.config.js production

Monitoring with PM2

PM2 offers a web-based monitoring tool called PM2 Plus. It offers insights into application performance, including:

  • CPU and memory usage.
  • Application health and status.
  • Real-time logs and error tracking.

To use PM2 Plus, run:

pm2 plus

Also Read: NGINX Explained! Know its Working & What is it Used For?


Conclusion

Whether you are running a simple application or a complex multi-instance setup, PM2 ensures stability, scalability, and ease of management. With features like clustering, detailed monitoring, and automated deployments, it’s an amazing tool for any Node.js developer or system administrator.