PM2: Monitoring and Managing JavaScript Applications

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is PM2?

PM2 (Process Manager 2) is an advanced process manager for Node.js applications that provides built-in monitoring, automatic restart, and load balancing capabilities. It allows you to run JavaScript applications in a production environment, track their status, collect performance metrics, and manage multiple processes simultaneously.

Why is monitoring needed?

In a production environment, it is critically important to know what is happening with the application: whether it has crashed, whether memory consumption is exceeded, or whether errors are occurring. PM2 solves these tasks by providing:

  • Automatic restart on failures
  • CPU and RAM metric collection
  • stdout/stderr logging
  • Built-in web monitoring interface (PM2 Plus)

Installation

Install PM2 globally via npm:

npm install -g pm2

Main features

  • Start an application: pm2 start app.js
  • Real-time monitoring: pm2 monit
  • View process list: pm2 list
  • Logs: pm2 logs
  • Web dashboard: pm2 plus (registration required)

JavaScript code example (Node.js)

Create a simple application app.js:

const http = require('http');const server = http.createServer((req, res) => {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.end('Hello from PM2!');});

server.listen(3000, () => { console.log('Server running on port 3000');});

Run it with monitoring:

pm2 start app.js --name my-apppm2 monit

You will see live graphs of memory and CPU consumption in the console, as well as the number of restarts.

When to use

PM2 is ideal for:

  • Node.js production servers
  • Microservice architectures
  • Applications requiring automatic recovery after failures
  • Scenarios where simple monitoring is needed without external services

Conclusion

PM2 is a must-have tool for any Node.js developer who wants to ensure the stable operation of their applications. It is easy to install, requires no complex configuration, and provides all the necessary functions for monitoring and process management.

Recommendations