Catalog / PM2 Cheatsheet
PM2 Cheatsheet
A comprehensive cheat sheet for PM2, a process manager for Node.js applications. Covers essential commands for managing, monitoring, and deploying applications with PM2.
PM2 Basics
Starting Applications
|
Start a Node.js application. Example: |
|
Start an application with a specific name. Example: |
|
Start an application in cluster mode, utilizing all available CPU cores. Example: |
|
Start an application in cluster mode with a specified number of instances. Example: |
|
Start an application using npm. Example: |
|
Automatically restart the app when files change. Example: |
|
Ignore specific paths when watching for file changes. Example: |
|
Start applications defined in an ecosystem file. Example: |
Managing Processes
|
Stop a specific process by name or ID. Example: |
|
Restart a specific process by name or ID. Example: |
|
Delete a specific process by name or ID. Example: |
|
Stop all running processes. Example: |
|
Restart all running processes. Example: |
|
Delete all processes from PM2. Example: |
|
Reload all processes, minimizing downtime. Example: |
Monitoring and Logs
|
List all processes managed by PM2. Example: |
|
Show detailed information about a specific process. Example: |
|
Open a real-time process monitoring dashboard. Example: |
|
Display logs for a specific process. Example: |
|
Show aggregated logs for all processes. Example: |
|
Clear all PM2 log files. Example: |
|
Reload all PM2 logs. Example: |
Ecosystem Configuration
Ecosystem File Structure
An Example
|
Common Ecosystem Options
|
Name of the application. Example: |
|
Path to the application script. Example: |
|
Number of application instances. Use Example: |
|
Automatically restart the application if it crashes. Example: |
|
Enable file watching for automatic restarts on code changes. Example: |
|
Maximum memory usage before restarting the application. Example: |
|
Environment variables for all environments. Example: |
|
Environment-specific variables (e.g., Example: |
Deploying with Ecosystem
|
Setup the production environment. Example: |
|
Update the production environment with new code. Example: |
|
Deploy the application to the production environment. Example: |
|
Revert to a previous deployment (n = number of revisions back). Example: |
|
Generate a sample Example: |
Advanced PM2 Usage
Startup Management
|
Generate a startup script to automatically start PM2 and managed processes on system boot. Example: |
|
Save the current process list to be restored on system boot. Example: |
|
Disable PM2 from starting on boot (remove the startup script). Example: |
|
Restore previously saved processes. Example: |
Process Interaction
|
Send a specific signal to a process. Example: |
|
Trigger a custom event in a PM2 managed process, if the application is set up to listen for the event. Example: |
|
Check if the PM2 daemon is running. Example: |
|
Pull new updates. Example: |
Module Management
|
Install a PM2 module. Example: |
|
Uninstall a PM2 module. Example: |
|
Will also display modules list. Example: |
Troubleshooting and Tips
Common Issues
If an application fails to start, check the PM2 logs for error messages using If the application is not automatically restarting after a crash, check the |
Useful Tips
Use an ecosystem file to manage application configurations. It provides a centralized way to define application settings, environment variables, and deployment options. Utilize PM2 modules for extended functionality. For example, |
Monitor application performance using Configure environment-specific variables in the ecosystem file to easily switch between development, staging, and production environments. |
Updating PM2
|
Update PM2 to the latest version, ensuring the daemon and all managed processes are updated. Example: |
|
Check the current PM2 version. Example: |