Skip to content
tezvyn:

PM2: The Process Manager for Production Node.js

Source: pm2.keymetrics.ioEasyHow cards are made

PM2: The Process Manager for Production Node.js

PM2 is a process manager that keeps your Node.js apps online. Use it to automatically restart crashed apps, run them in the background, and scale across CPU cores. The footgun is forgetting to run pm2 save to make your process list survive server reboots.

Why it exists

Running node app.js directly in a terminal is fragile. If the application crashes, it stays down. If you close the terminal, the process dies. For a production application that needs to be available 24/7, you need a separate system to manage the application's process and ensure it remains online.

The mental model

PM2 is a daemon process manager. Think of it as a dedicated supervisor for your application processes. Instead of you directly running node app.js, you tell PM2 to run it. PM2 then starts your app in the background, watches over it, and automatically restarts it if it crashes or runs out of memory, ensuring high availability.

How it works

You install PM2 globally via npm and use its command-line interface. The command pm2 start app.js starts your application and daemonizes it, meaning it runs in the background, detached from your terminal. PM2 maintains a process list you can view with pm2 list. You can then manage your apps using commands like pm2 restart, pm2 stop, and pm2 delete. For persistence across server reboots, you run pm2 startup once to generate a system startup script, then pm2 save anytime you want to freeze the current list of running applications for automatic respawning.

When to use it

Use PM2 for any Node.js application in a production or staging environment that needs to be long-running, like web servers, APIs, or background workers. Its cluster mode (pm2 start app.js -i max) is invaluable for instantly scaling a stateless app to use all available CPU cores, improving performance without changing application code. The --watch flag is also useful in development for automatically restarting the server on file changes.

When not to use it

PM2 is often overkill for simple, one-off scripts. In modern containerized environments like Docker and Kubernetes, its role is diminished. While you can run PM2 inside a container, the container orchestrator (like Kubernetes) is typically responsible for health checks, scaling, and restarts, making PM2's core supervision features redundant. In those cases, running the node process directly is often simpler.

One canonical example

A common use case is deploying a Node.js API. After deploying your code to a server, you would run pm2 start server.js --name my-api -i max. This command starts the application, names it "my-api" for easy management, and uses the -i max flag to launch a process on each available CPU core, with PM2 automatically load-balancing requests between them. To ensure it survives reboots, you would then run pm2 startup followed by pm2 save.

Interview question

Which command is essential for PM2 to remember and automatically restart your applications after a server reboot?

  • a.pm2 saveCorrect
  • b.pm2 start app.js
  • c.pm2 list
  • d.pm2 startup
Why?

The card states that "pm2 save anytime you want to freeze the current list of running applications for automatic respawning" after a reboot. While "pm2 startup" generates the necessary system script, it's "pm2 save" that records which applications should be managed by that script.

Just read this? Test yourself on what you have been reading.

Read the original → pm2.keymetrics.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles