que-es-como-usar-pm2

How to manage Node.js processes with PM2

  • 3 min

PM2 is a process manager for Node.js applications that allows you to manage, monitor, and maintain applications in production.

Generally, when we develop Node.js applications, we run them directly with:

node app.js

But this has several problems in production. For example, if the application crashes, it won’t restart, we have no easy way to monitor the status, etc.

PM2 solves all these problems by providing:

  • Auto-restart of applications when they crash
  • Monitoring and logs in real-time
  • Load balancing across multiple instances

Notice about AGPLv3 license:
PM2 is distributed under the AGPLv3 license (GNU Affero General Public License). There could be potential obligations if:

  • You deeply integrate PM2 into your application (creating derivative work)
  • You offer PM2 as a public SaaS service (would require releasing modifications)

Installing PM2

To use PM2, we install it globally using NPM:

npm install -g pm2

Now we can verify that it has been installed correctly by running:

pm2 --version

Basic usage of PM2

Starting an application

The basic command to start an application with PM2 is:

pm2 start app.js

We can also specify a name for the application:

pm2 start app.js --name "my-api"

PM2 automatically daemonizes the process (runs it in the background)

Essential commands

Here is a list of the main commands available in PM2.

CommandDescription
pm2 listLists all managed applications
`pm2 show <idname>`
`pm2 stop <idname>`
`pm2 restart <idname>`
`pm2 delete <idname>`
pm2 logsDisplays real-time logs
pm2 monitInteractive monitor

And here is an example of its usage,

# Start application
pm2 start server.js --name "api-server"

# Check status
pm2 list

# View logs
pm2 logs api-server

# Stop application
pm2 stop api-server

# Delete application
pm2 delete api-server

Startup script

To ensure that PM2 starts automatically when the system reboots, we can do:

pm2 startup
pm2 save

This will create an appropriate startup script for your operating system.

On Linux systems, the pm2 startup command should be executed with the appropriate permissions, usually via sudo

Cluster mode

Node.js is single-threaded by default. PM2 allows you to easily create a cluster of processes:

pm2 start app.js -i max

This will create one instance for each available CPU, sharing the same port.

The cluster mode only works for stateless applications. If you use in-memory sessions, you will need a solution like Redis.

Monitoring and logs

PM2 includes a built-in monitor:

pm2 monit

pm2-monitor

Additionally, PM2 automatically manages the logs:

  • ~/.pm2/logs/app-out.log: Standard logs
  • ~/.pm2/logs/app-error.log: Error logs

Some commands

# View logs in real-time
pm2 logs

# Flush logs
pm2 flush

# Specific app logs
pm2 logs app-name

Advanced configuration with ecosystem files

For more complex configurations, PM2 supports configuration files (ecosystem.config.js):

module.exports = {
  apps: [{
    name: "api",
    script: "./src/index.js",
    instances: 2,
    exec_mode: "cluster",
    env: {
      PORT: 3000,
      NODE_ENV: "development"
    },
    env_production: {
      PORT: 80,
      NODE_ENV: "production"
    }
  }]
}
  • instances: Number of instances (can be a number or “max” to use all CPUs)
  • exec_mode: “cluster” or “fork”
  • env: Environment variables
  • watch: Automatically restart on detecting changes
  • error_file and out_file: Paths for the logs