LinuxCommandLibrary

pm2

Manage and run Node.js applications

TLDR

Start a process with a name that can be used for later operations

$ pm2 start [app.js] --name [application_name]
copy

List processes
$ pm2 list
copy

Monitor all processes
$ pm2 monit
copy

Stop a process
$ pm2 stop [application_name]
copy

Restart a process
$ pm2 restart [application_name]
copy

Dump all processes for resurrecting them later
$ pm2 save
copy

Resurrect previously dumped processes
$ pm2 resurrect
copy

SYNOPSIS

pm2 command [options] arguments
Examples:
pm2 start app|script [options]
pm2 list
pm2 restart app|id
pm2 stop app|id
pm2 delete app|id
pm2 monit
pm2 save

PARAMETERS

--name
    Assign a custom name to the process.

-i , --instances
    Launch multiple instances (cluster mode) of the application. 'max' uses all CPU cores.

--watch
    Watch application folder and restart on file changes.

--ignore-watch
    Prevent specified paths/patterns from being watched when --watch is enabled.

--output
    Specify output log file path.

--error
    Specify error log file path.

--log-date-format
    Specify the date format for logs (e.g., 'YYYY-MM-DD HH:mm:ss').

--max-memory-restart
    Restart the application if its memory usage exceeds the specified threshold (e.g., '200M', '1G').

--env
    Specify an environment name to load variables from ecosystem file (e.g., --env production).

--no-daemon
    Do not daemonize PM2; run it in the foreground (useful for Docker).

--silent, -s
    Do not output any logs from PM2 itself (only application logs).

--force
    Force an action, for example, start an app even if it's already declared.

DESCRIPTION

pm2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime, and to facilitate common system administration tasks. Beyond Node.js, it supports any language or framework (Python, PHP, Go, .NET, etc.). It monitors applications, manages logs, and provides a clustering mode to distribute load across CPU cores. It simplifies deployment, offers a comprehensive CLI, and integrates with advanced monitoring solutions. It's designed for reliability, scalability, and ease of use in production environments.

CAVEATS

pm2 can sometimes consume significant memory if managing a very large number of processes or if log rotation is not adequately configured for high-volume logs. It's not a full-fledged deployment tool but rather a process manager, often used in conjunction with other deployment automation tools like Ansible or Capistrano. While it can manage applications written in any language, pm2 itself requires a Node.js runtime environment to operate.

ECOSYSTEM FILE

An ecosystem file (ecosystem.config.js/json/yml) is a declarative configuration that allows you to define one or more applications, their environment variables, logging paths, and other pm2 options. It's the recommended way to manage complex deployments, enabling version control of application configurations and simplifying deployment processes.

ZERO-DOWNTIME RELOADS

Achieved via the pm2 reload app|id command. pm2 starts new instances of your application, waits for them to be ready, and only then gracefully kills the old instances. This ensures continuous service availability during application updates, making it ideal for critical production systems.

CLUSTERING MODE

Using pm2 start app.js -i max (or a specific number), pm2 launches as many instances of your application as there are CPU cores (or the specified number). pm2 automatically load-balances incoming requests across these instances, significantly improving performance and reliability for CPU-bound applications.

HISTORY

pm2 was created by Alexandre Strzelewicz and initially released around 2013. It rapidly gained popularity within the Node.js community as a robust and feature-rich solution for managing applications in production environments. Its continuous development has expanded its capabilities to support applications written in virtually any language, solidifying its position as a widely adopted process manager.

SEE ALSO

systemctl(1), nohup(1), screen(1), tmux(1), supervisor(1), forever(1)

Copied to clipboard