Process Management & PM2
A Process is simply a program in execution. Every time you run a script, Linux assigns it a PID (Process ID). As a "Master" developer, you need to know how to track, stop, and automate these processes.
1. Basic Linux Process Commandsโ
Before using specialized tools, you should know the built-in Linux commands:
top: Displays a real-time view of running processes, CPU usage, and RAM. (Pressqto exit).ps aux: Shows a snapshot of every process running on the system.kill <PID>: Stops a specific process.killall node: Forcefully stops all running Node.js processes.&: Adding an ampersand after a command (e.g.,node server.js &) runs it in the background.
2. The Problem with "Background" Tasksโ
Running a process in the background using & is a start, but itโs not enough for production.
- If the app crashes, it stays dead.
- If the server reboots, the app doesn't start back up.
- You can't easily see the logs.
3. Enter PM2 (Process Manager 2)โ
PM2 is the industry standard for Node.js process management. It ensures your CodeHarborHub projects stay online forever.
Installationโ
npm install -g pm2
Essential PM2 Commandsโ
pm2 start server.js --name "my-app": Starts your app and gives it a name.pm2 list: Shows the status (online/stopped), CPU, and memory of all apps.pm2 logs: Displays real-time logs (crucial for debugging!).pm2 stop <id>: Stops the app.pm2 restart <id>: Restarts the app.pm2 delete <id>: Removes the app from the list.
4. Zero-Downtime & Self-Healingโ
One of the "Master" features of PM2 is the ability to monitor your app. If your Node.js code hits an error and crashes, PM2 will instantly restart it.
Ecosystem Fileโ
For professional setups, we use a configuration file (ecosystem.config.js) instead of long commands:
module.exports = {
apps : [{
name: "CodeHarborHub-API",
script: "./server.js",
instances: "max", // Utilizes all CPU cores (Cluster Mode)
exec_mode: "cluster",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}
5. Startup Scripts: The Final Stepโ
If your server (AWS, DigitalOcean, etc.) reboots for maintenance, you want your apps to start automatically.
- Run
pm2 startup. - Copy and paste the command Linux gives you.
- Run
pm2 save.
This "freezes" your current process list and tells Linux to resurrect them on every boot.
Practice: The Persistence Testโ
- Start a simple Node.js script using
pm2 start app.js. - Find the PID of that process using
ps aux | grep node. - Manually kill that PID using
kill -9 <PID>. - Run
pm2 list. You will see that PM2 noticed the "crash" and automatically restarted the app!
Run pm2 monit to open a beautiful dashboard inside your terminal that shows real-time performance metrics for all your running applications.