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]

PARAMETERS

start <app_name | script_path | ecosystem.config.js>
    Starts an application. Can be a script, an application name or a PM2 ecosystem file.

stop <app_name | id | all>
    Stops one or all applications.

restart <app_name | id | all>
    Restarts one or all applications.

delete <app_name | id | all>
    Deletes one or all applications from PM2's process list.

list
    Lists all applications managed by PM2.

show <app_name | id>
    Shows information about a specific application.

logs <app_name | id | all>
    Displays logs for a specific application or all applications.

monit
    Opens a console-based monitoring interface.

kill
    Kills the PM2 daemon.

update
    Updates PM2 to the latest version.

startup <systemd | ubuntu | centos | darwin>
    Generates a startup script to automatically start PM2 and managed applications on system boot.

save
    Saves the current process list to automatically restart applications on reboot.

resurrect
    Restores previously saved process list.

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, reload them without downtime, and facilitates common system admin tasks. PM2 simplifies deployment, scaling, and monitoring of Node.js applications. It's designed for production environments, ensuring high availability and optimal performance. PM2 monitors resource usage (CPU, memory) and automatically restarts applications if they crash or encounter errors. It also provides logging capabilities, enabling you to track application behavior and diagnose issues. PM2 can be controlled from the command line, or via a graphical interface (PM2 Plus, which is a commercial offering). It's a widely used tool in the Node.js ecosystem for managing and deploying applications effectively.

CAVEATS

PM2 requires Node.js to be installed. Ecosystem files offer more advanced configuration options than command-line arguments.
PM2 Plus is a paid service offering advanced monitoring and management capabilities.

<B>ECOSYSTEM FILES</B>

Ecosystem files (usually named ecosystem.config.js or ecosystem.config.json) allow you to define application configurations, environment variables, and deployment settings in a structured way. This is the recommended approach for managing applications in production.

<B>CLUSTER MODE</B>

PM2's cluster mode enables you to run multiple instances of your application to utilize all available CPU cores. This improves performance and scalability. You can enable cluster mode using the -i <instances> flag with the start command.

<B>ZERO DOWNTIME RELOADS</B>

PM2 offers zero downtime reloads, ensuring that your application remains available to users during restarts. This is achieved by starting new instances before stopping the old ones.

HISTORY

PM2 was created to address the need for a robust and reliable process manager for Node.js applications in production environments. It gained popularity due to its ease of use, powerful features, and focus on high availability. Over time, PM2 has evolved to include features like load balancing, monitoring, and cluster mode, making it a comprehensive solution for managing Node.js applications.

SEE ALSO

node(1), npm(1)

Copied to clipboard