PM2 commands for managing and monitoring Node.js applications:
-
Start an application:
pm2 start app.js
Starts a Node.js app (
app.js
), creating a managed process. -
Start with a specific name:
pm2 start app.js --name "myApp"
-
Stop an application:
pm2 stop <app_name or id>
-
Restart an application:
pm2 restart <app_name or id>
-
Delete an application:
pm2 delete <app_name or id>
-
Seeing all running applications:
pm2 status
-
List all running applications:
pm2 list
-
View application logs:
pm2 logs
-
View logs for a specific app:
pm2 logs <app_name or id>
-
Display details of a specific app:
pm2 show <app_name or id>
-
Monitor all running apps in real-time:
pm2 monit
- Scale an application:
This command allows you to increase or decrease the number of instances.
pm2 scale <app_name or id> <number>
-
Start with environment variables:
pm2 start app.js --env production
-
Start an application with a JSON config file:
pm2 start ecosystem.config.js
-
Save the current process list:
pm2 save
This will allow PM2 to resurrect the saved processes on server reboot.
-
Reload saved process list on startup:
pm2 resurrect
-
Enable auto-restart on file changes:
pm2 start app.js --watch
-
Specify files or folders to watch:
pm2 start app.js --watch --ignore-watch="node_modules"
- Generate a startup script:
This command generates the command to make PM2 start on boot. Run the output command to enable it.
pm2 startup
- Reload all applications with zero downtime:
This command is especially useful for reloading apps without downtime (great for production).
pm2 reload all
-
Update PM2 to the latest version:
pm2 update
-
Reload PM2’s daemon process:
pm2 updatePM2
-
Get system information (CPU, RAM usage):
pm2 info <app_name or id>
-
Clear logs for all applications:
pm2 flush
-
Kill the PM2 daemon (stops all applications and the PM2 process):
pm2 kill
Basic JSON configuration file (ecosystem.config.js
) for managing two applications using PM2. This file includes environment variables, watch mode, and other configurations. One application is a Node.js (Express) app, and the other is a Python app.
module.exports = {
apps: [
{
name: "expressApp", // Name of the first app
script: "app.js", // Entry file for the Node.js app
watch: true, // Watch for file changes
env: { // Environment variables for development
NODE_ENV: "development",
PORT: 3000,
DB_URI: "mongodb://localhost:27017/devdb"
},
env_production: { // Environment variables for production
NODE_ENV: "production",
PORT: 8080,
DB_URI: "mongodb://localhost:27017/proddb"
},
ignore_watch: ["node_modules"], // Ignore changes in the node_modules folder
instances: 2, // Run 2 instances (for load balancing)
exec_mode: "cluster" // Use cluster mode for zero-downtime reloads
},
{
name: "pythonApp", // Name of the second app
script: "python", // Command to run Python
args: "app.py", // Script file for the Python app
interpreter: "python3", // Specify Python 3 interpreter
watch: ["./"], // Watch for changes in the root directory
env: { // Environment variables for development
ENV_MODE: "development",
API_KEY: "dev_api_key"
},
env_production: { // Environment variables for production
ENV_MODE: "production",
API_KEY: "prod_api_key"
}
}
]
};
name
: Name of the application, which you can use with PM2 commands.script
: Specifies the script or command to start the application (app.js
for the Express app,app.py
for the Python app).watch
: Enables PM2's file-watching feature to restart the app if files change.ignore_watch
: Prevents PM2 from restarting on changes in specified folders (likenode_modules
).instances
: Sets the number of instances (useful for load balancing).exec_mode
: When set to"cluster"
, PM2 will run the app in cluster mode, allowing zero-downtime restarts.env
andenv_production
: Defines environment variables for development and production modes.