zaro

What is PM2 commands?

Published in PM2 Management 3 mins read

PM2, a powerful and feature-rich process manager for Node.js applications, provides a comprehensive set of command-line interface (CLI) tools to manage, monitor, and deploy applications efficiently. These commands allow developers to keep applications alive indefinitely, manage logging, enable clustering, and ensure high availability.

Essential PM2 Commands

While PM2 boasts a wide array of functionalities, some core commands are fundamental to its operation. The following table highlights some of these essential commands:

Command Name Description
pm2 ping Pings the PM2 daemon. If the daemon is not running, this command will initiate its launch.
pm2 updatePM2 Updates the in-memory PM2 process with the locally installed PM2 version. This ensures that the running PM2 daemon benefits from recent updates without needing a full restart.
pm2 update An alias for pm2 updatePM2, serving the same purpose of updating the in-memory PM2 instance with the local version.
pm2 install <module|git:/|options>
pm2 module:install <module|git:/|options>
Installs or updates a PM2 module (or a set of modules) and ensures it runs persistently. Modules extend PM2's capabilities, offering integrations or additional features.

Other Key PM2 Commands for Application Management

Beyond the core commands listed above, PM2 offers a wide array of functionalities to manage applications throughout their lifecycle. These commands are crucial for starting, stopping, monitoring, and maintaining Node.js applications.

Process Management

  • pm2 start <app_name|script>: Launches an application. This is the most fundamental command for deploying an app with PM2.
    • Example: pm2 start app.js --name my-api
  • pm2 stop <app_name|id|all>: Stops a specific application, by its name or ID, or all running applications.
  • pm2 restart <app_name|id|all>: Restarts a specific application or all applications.
  • pm2 reload <app_name|id|all>: Performs a zero-downtime reload of an application. This is ideal for deployments as it brings up new instances before taking down old ones, ensuring continuous service.
  • pm2 delete <app_name|id|all>: Stops and removes an application from the PM2 process list.
  • pm2 list (or pm2 ls): Displays a list of all managed applications, including their status, uptime, and other crucial metrics.

Monitoring and Logging

  • pm2 logs <app_name|id>: Shows the real-time logs of a specific application.
    • Example: pm2 logs my-api --lines 100
  • pm2 monit: Opens a dashboard-like interface in the terminal to monitor CPU, memory, and other performance metrics for all managed applications.

System Integration and Setup

  • pm2 startup: Generates and configures a system startup script (e.g., systemd, Upstart, launchd) to ensure PM2 and its managed applications automatically restart when the server reboots.
  • pm2 save: Saves the current list of running applications, so they can be resurrected after a system reboot or pm2 kill.
  • pm2 unstartup: Disables and removes the PM2 startup script from the system.
  • pm2 kill: Kills the PM2 daemon, stopping all managed processes.

Understanding these commands is essential for effectively leveraging PM2 to manage Node.js applications in development and production environments, ensuring high availability and robust performance.