Mastering Node.js Application Management with PM2 - The basics

Managing a multi-application pipeline, such as daily-digest, in a production environment can be challenging. Ensuring your applications run smoothly, efficiently, and recover from crashes without manual intervention is crucial. This is where PM2, a popular process manager for Node.js, comes into play. PM2 simplifies the management of Node.js applications and offers features that enhance performance and reliability.

Before we beginn...

This post is the first in a series on using PM2 for deploying multiple Node applications to a remote server. We will cover:

In today's post, we'll cover the first two points. Note that deployment methods for Node.js vary depending on your chosen cloud vendor, such as AWS Beanstalk Vercel, Heroku, etc. PM2 offers integrations for almost all cloud platforms (see https://pm2.io/docs/plus/integration/).

In our case, we opted to run everything on our own Linux server, giving us full control to run both Node instances and our LLMs locally.

What is PM2?

PM2 is a production-grade process manager for Node.js applications. It ensures your applications remain alive, reload without downtime, and manage performance aspects. PM2 excels in deployments, making it easy to deploy multiple Node.js instances to a remote server. Combined with GitHub Actions, it forms a lightweight CD pipeline.

Key Features of PM2

  1. Process Management: Start, stop, restart, and delete processes with ease using a simple CLI.
  2. Cluster Mode: Run your application in cluster mode to leverage multi-core systems
  3. Load Balancing: Automatically balances the load across all instances of your application.
  4. Monitoring: Real-time monitoring of applications, including CPU and memory usage.
  5. Logging: Manages application logs, including error logs and access logs.
  6. Startup Scripts: Ensures that your applications start on system boot.
  7. Watch & Reload: Automatically restarts applications on error

Installing PM2

Install PM2 globally on both the remote machine and your computer using npm:

npm install -g pm2

Basic Usage

Once PM2 is installed, you can start managing your Node.js applications. Here’s a quick overview of the basic commands:

  1. Start an Application:

    pm2 start app.js

    This command starts your application and assigns it an ID for management.

  2. List All Processes:

    pm2 list

    Displays a list of all managed applications along with their status.

  3. Stop an Application:

    pm2 stop app.js

    Stops the specified application.

  4. Restart an Application:

    pm2 restart app.js

    Restarts the specified application.

  5. Delete an Application:

    pm2 delete app.js

    Removes the specified application from PM2’s process list.

Advanced Features

Monitoring

Even the free version of PM2 provides a robust monitoring dashboard. To use it, register at pm2.io. After registering, click the "Connect" button on the top right, which opens a popup.

Run the command displayed in the popup on the machine running the Node process, and it will appear in your dashboard. This allows you to stream logs, see current CPU/Memory usage, and more, providing a quick overview.

Log Management

PM2 handles logs for you, making it easy to view and manage log files.

Auto-Restart on File Changes

During development, you might want PM2 to restart your application whenever a file changes.

pm2 start app.js --watch

The --watch flag tells PM2 to monitor the files in your application directory and restart the application if any changes are detected.

Startup Scripts

To ensure your applications start automatically when the server reboots, you can generate and configure startup scripts.

pm2 startup pm2 save

The pm2 startup command generates the necessary script for your operating system, and pm2 save saves the current process list.

Conclusion

PM2 is a powerful tool that simplifies the management of Node.js applications, providing essential features such as process management, monitoring, load balancing, and more. By leveraging PM2, you can ensure that your applications run smoothly, efficiently, and reliably in a production environment. Whether you’re managing a single application or multiple services, PM2 offers the tools you need to keep everything running seamlessly.

In the next blog post, we will explore PM2's deployment capabilities and how to leverage GitHub Actions to build a lightweight CD pipelines.