development
management
process
tips and tricks
auto-restart, backend-development, devops, keep-app-alive, linux-process-manager, nodejs, nodejs-apps, nodejs-deployment, nodejs-monitoring, nodejs-performance, nodejs-production, nodejs-server, nodejs-tools, pm2, pm2-cluster, pm2-guide, pm2-tutorial, process-manager, production-ready, server-management
9M2PJU
0 Comments
How PM2 Works: The Process Manager That Keeps Your Apps Alive
Have you ever run a Node.js app on a server, only to watch it crash a few hours later due to an unexpected error? Or maybe you’ve used nohup
or screen
to keep a script running in the background, only to forget it exists?
That’s where PM2 comes in—a smart, modern process manager designed to make your life easier by keeping your apps alive, managing logs, and even auto-starting your apps on system boot.
In this post, let’s break down how PM2 works, what it can do, and why it’s such a popular tool in the Node.js and backend developer world.
💡 What Is PM2?
PM2 (short for “Process Manager 2”) is a lightweight, open-source process manager for Node.js applications—but it also works with any other executable script (like Python, Bash, Go, PHP).
It helps you:
- Keep apps running forever
- Restart automatically on crashes
- Monitor performance (CPU, RAM)
- View logs easily
- Auto-start apps on server reboot
- Load-balance across CPU cores
And best of all—it’s just a single command-line tool you install globally:
npm install -g pm2
🛠️ How Does PM2 Work?
PM2 runs as a daemon in the background. When you start an app with PM2, it doesn’t just run your script—it:
- Forks your app into its own background process
- Watches that process for crashes or high memory use
- Restarts it if it dies or exceeds limits
- Logs everything to disk for easy debugging
- Keeps track of which apps should be auto-started on boot
This means you can close your SSH session or even reboot your server—and PM2 will bring your apps back to life without you doing anything.
🔁 The Lifecycle of an App in PM2
Here’s what happens step-by-step when you run:
pm2 start app.js
- PM2 forks your app into a child process
- It records the process in its internal list
- PM2 monitors the process: CPU, memory, uptime, logs
- If the app exits unexpectedly, PM2 immediately restarts it
- If you reboot the server, PM2 can reload it automatically (after you run
pm2 startup
andpm2 save
once)
This makes it ideal for production use, or even for bots, APIs, or any long-running script.
🧰 Key Features of PM2
✅ Keeps Apps Running Forever
Your app crashes at 2am? PM2 restarts it instantly—no downtime.
🚀 Auto-Start on Reboot
Set up a startup script so PM2 restores all your apps after a server reboot:
pm2 startup
pm2 save
📊 Real-Time Monitoring
pm2 monit
View live CPU/RAM usage for each app.
📜 Centralized Logs
pm2 logs
No more digging around for log files. PM2 keeps logs per app and shows them live.
🌐 Load Balancing with Clusters
You can spawn multiple instances of your app to utilize all CPU cores:
pm2 start app.js -i max
Perfect for improving performance and resilience.
🔗 PM2 Works with More Than Node.js
Even though PM2 was built for Node.js, you can run any type of app or script:
- Python:
pm2 start script.py --interpreter python3
- Bash:
pm2 start myscript.sh
- PHP:
pm2 start app.php --interpreter php
It’s very flexible and language-agnostic.
🤔 When Should You Use PM2?
- Running a backend API or server
- Hosting a long-running bot or process
- Deploying Node.js, Python, or Go scripts
- Wanting crash recovery without systemd or Docker
- Needing one-liner control for logs, restarts, uptime
If you’re running an app for more than a few minutes—it’s worth using PM2.
🧼 Wrapping Up
PM2 is like a smart assistant for your apps—it keeps them running, watches them, restarts them if anything goes wrong, and gives you tools to manage everything with a few simple commands.
Whether you’re managing one app or ten, PM2 gives you peace of mind and control in a way that nohup
, screen
, or manual scripts just can’t.
It works quietly in the background so your app doesn’t have to shout for help.
Post Comment