PM2 For NodeJS Auto Run and Watch
PM2 (Process Manager 2) is a popular production process manager for Node.js applications. It helps you manage and keep your applications online 24/7, providing a number of useful features for managing application processes, monitoring performance, and ensuring stability. Here's a comprehensive overview of PM2:
Key Features of PM2
Process Management: PM2 allows you to start, stop, restart, and manage multiple applications or microservices easily.
Load Balancing: With PM2, you can take advantage of clustering to maximize your application's performance by distributing the load across multiple CPU cores.
Monitoring: PM2 provides built-in monitoring tools to keep track of application performance, memory usage, and CPU usage in real-time.
Log Management: PM2 handles log management by aggregating and rotating logs for all managed applications. It also supports custom log paths and integrates with log management systems.
Startup Scripts: PM2 can generate startup scripts to ensure your applications start automatically when the server reboots.
Deployment: PM2 includes deployment features to automate the deployment process of your applications.
Configuration Management: PM2 supports JSON and YAML configuration files to define application settings and environment variables.
Graceful Reload: PM2 provides zero-downtime reloads by allowing you to reload your applications without downtime.
Integrated Watchdog: Automatically restarts your applications if they crash or become unresponsive.
Installation
You can install PM2 globally using npm:
npm install -g pm2
Basic Commands
Start an application:
pm2 start app.js
List all processes:
pm2 list
Stop a process:
pm2 stop app
Restart a process:
pm2 restart app
Delete a process:
pm2 delete app
Show logs:
pm2 logs
Process Monitoring
You can monitor your applications in real-time:
pm2 monit
This command opens an interactive interface showing CPU and memory usage for each process.
Cluster Mode
PM2 allows you to run your application in cluster mode to take advantage of multi-core systems:
pm2 start app.js -i max
The -i max
option tells PM2 to automatically detect the number of available CPUs and run one instance per CPU core.
Configuration Files
PM2 can be configured using JSON or YAML files. Here's an example of a JSON configuration file (ecosystem.config.json
):
{
"apps": [
{
"name": "my-app",
"script": "./app.js",
"instances": "max",
"exec_mode": "cluster",
"env": {
"NODE_ENV": "development"
},
"env_production": {
"NODE_ENV": "production"
}
}
]
}
You can start the application using the configuration file:
pm2 start ecosystem.config.json
Log Management
PM2 handles logs for all processes. You can view logs with:
pm2 logs
To specify custom log files:
pm2 start app.js --log-date-format="YYYY-MM-DD HH:mm Z" -o out.log -e err.log
Startup Script
Generate a startup script to ensure PM2 restarts your applications on server reboot:
pm2 startup
pm2 save
The pm2 startup
command generates a startup script, and pm2 save
saves the current process list.
Deployment
PM2 simplifies deployment with built-in support for deploying applications:
Create a deployment configuration file (ecosystem.config.js
):
module.exports = {
apps: [{
name: "my-app",
script: "./app.js"
}],
deploy: {
production: {
user: "node",
host: "server.com",
ref: "origin/main",
repo: "git@repository.git",
path: "/var/www/my-app",
"post-deploy": "npm install && pm2 reload ecosystem.config.js --env production"
}
}
}
Deploy your application:
pm2 deploy ecosystem.config.js production
Conclusion
PM2 is a powerful tool for managing Node.js applications in production. It simplifies process management, load balancing, monitoring, and deployment, helping you keep your applications stable and performant. With PM2, you can ensure your applications stay online 24/7 and handle traffic efficiently.
Subscribe to my newsletter
Read articles from Mutual Fund Developer directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mutual Fund Developer
Mutual Fund Developer
๐ Innovative Software Engineer | Full-Stack Developer Greetings! I'm a seasoned software engineer with 4 years of experience spearheading the development of cutting-edge solutions for diverse industries. My passion lies in crafting robust software architectures and leading trans-formative projects that elevate operational efficiency and drive business growth. ๐ What expertise in these segment Fin-tech (Mutual Fund) Project development experience KYC check system build own Build new ideas to easy work for team BSE Mutual Fund API setup Build innovative ideas to low costing of project via create automation of task. ๐ What i Want I like to work with new ideas that build amazing something new for mass users. Learn from problem to get solution Supercharge existing work style I love to work with agile workflow ๐ Expertise Highlights: Full-Stack Mastery: Proficient in crafting seamless web and app experiences using PHP, Laravel, Node.js, Flutter, and Angular. Strategic Project Management: Skilled in orchestrating end-to-end project lifecycles, from ideation and planning to deployment and optimization. Innovative Problem Solver: Dedicated to devising creative solutions that address complex challenges and deliver tangible results. ๐ง Technical Skills Snapshot: Languages: PHP, JavaScript (Node.js), API: API development, Frameworks: Laravel, Angular, Tools & Technologies: Docker, Git, RESTful APIs ๐ข Experience: I've championed the development and implementation of game-changing solutions that have streamlined processes and enhanced productivity across departments. I thrive in agile environments, leveraging strong communication and collaboration skills to lead cross-functional teams toward shared goals. I re-designed order flow after that 2cr SIP order managing. Mutual fund database management. ๐ฌ Let's Connect! I'm enthusiastic about staying ahead of industry trends and leveraging emerging technologies to create impactfull software solutions. If you're seeking a proactive and versatile software engineer to elevate your team, let's connect! Reach out to explore how we can collaborate on innovative projects that make a difference.