How to Keep Your Node Server Running Continuously with PM2 and Essential Commands


If you want your Node server to keep running without shutting down, PM2 is the simplest solution. Once installed, it allows you to manage processes—starting, restarting, checking logs, and automatic recovery after reboot—all in one place, making operations much smoother.
Especially during development, you might run a server with node app.js, but in production environments, closing the terminal often stops the server. PM2 keeps it running in the background, restarts easily if errors occur, and is widely chosen for real-world use.
Why You Need Basic PM2 Commands
PM2 isn't just a tool to launch servers—it's a process manager for Node.js applications. It lets you monitor multiple projects simultaneously, keeping your operations clean and organized.
For example, after developing an Express server and deploying it, running it directly with node might shut down if the SSH connection drops. With PM2, the server runs independently of the terminal session, ensuring stability.
It also supports cluster mode, which distributes CPU usage across multiple cores, making scaling easier when traffic increases. This versatility makes PM2 suitable for everything from small personal projects to basic production servers.
You can find more articles on similar topics in one place.
View Related ArticlesInstalling and Starting PM2 for the First Time
The first step is to install it globally. Typically, this is done with:
npm install -g pm2
If Node.js and npm are already installed, this process is straightforward.
To start a server file like app.js, run:
pm2 start app.js
The process will begin in the background. To check its status, use:
pm2 list
If your project uses environment variables, prepare them before starting. For example, specify a port with:
PORT=3000 pm2 start app.js
Common PM2 Commands for Daily Operations
You don't need many commands to manage most tasks. Mastering these basics covers almost all operations: starting, restarting, stopping, deleting, and checking status.
pm2 start app.js: Start the serverpm2 restart app.jsorpm2 restart id: Restart a processpm2 stop app.jsorpm2 stop id: Stop the serverpm2 delete app.jsorpm2 delete id: Remove from the listpm2 list: Check running processespm2 logs: View logs
The id refers to a number listed in pm2 list. Sometimes using the ID is more convenient than the name, especially when managing multiple projects. For instance, if you have two API servers, you can assign names like:
pm2 start app.js --name api-main
Then manage them with pm2 restart api-main.
Checking Logs and Diagnosing Issues
When a server fails to start or responds slowly, checking logs is crucial. PM2 manages both application and error logs, making troubleshooting much easier.
Running pm2 logs displays real-time logs. To view logs for a specific process, add its name or ID:
pm2 logs api-main
If an error occurs right after deployment, it's often found in these logs. For example, port conflicts will show messages like "Another process is using the same port." In such cases, clean up existing processes or change the port and restart.

Setting Up Automatic Restart After Reboot
A key advantage of PM2 is its ability to automatically restart your server after a reboot. This feature ensures it functions as a true production tool.
To enable this, first save the current process list with:
pm2 save
Then register startup with:
pm2 startup
Follow the instructions provided to complete setup. Once done, PM2 will automatically restore your processes after a reboot.
For example, on a Linux VPS, if the server reboots during maintenance, PM2 remembers its previous state and restarts the services. This is especially useful for small APIs or web apps in production.
Common Configuration Examples in Practice
While simple servers can be started with one command, adding more options makes an ecosystem configuration file helpful. It centralizes settings like ports, environment variables, execution mode, and instance counts.
Create a file like ecosystem.config.js, define your app name, entry file, and env values, then start it with:
pm2 start ecosystem.config.js
This approach keeps configurations code-like, making them easier to review during deployments. You can also use pm2 reload for near-zero-downtime updates in simple services, though this may not apply universally depending on your architecture.
Misunderstandings About PM2 Usage
New users often confuse the difference between restart and reload. restart fully restarts a process, while reload attempts to update more smoothly when possible.
Another common mistake is mixing up Node code errors with PM2 issues. PM2 manages processes but doesn't fix application logic—so if your server fails to start due to syntax errors in code, you must check the app logs first before blaming PM2.
For example, a SyntaxError means fixing the code is necessary. If memory issues arise, adjust instance counts or execution structure instead.
Key Points to Remember When Using PM2
You don't need to learn PM2 in depth. Mastering basics like starting, checking status, viewing logs, and restarting significantly reduces operational complexity. Adding automatic restarts and boot recovery makes it a reliable management solution.
This is especially valuable for solo developers or small projects, where using PM2's basic commands minimizes post-deployment anxiety. Even if you later move to Docker or other deployment tools, these foundational concepts remain relevant.
FAQ
Q1: Is PM2 always better than running directly with Node?
No, not necessarily. For local development, node app.js is sufficient. PM2 shines in production environments or when long-running processes are needed.
Q2: Does a server started with PM2 keep running even if the computer shuts down?
The server stops if the machine powers off. However, on remote servers, PM2 keeps processes alive regardless of terminal sessions ending.
Q3: What's the first command to memorize in PM2?
pm2 start, pm2 list, pm2 logs, and pm2 restart cover most needs. With these four, you can handle basic server operations.
Note: PM2 features may vary slightly by version, so always verify with your installed version before applying changes.
Keep exploring similar topics.
View Category ArticlesFAQ
Is PM2 always better than running directly with Node?
No, not necessarily. For local development, `node app.js` is sufficient. PM2 shines in production environments or when long-running processes are needed.
Does a server started with PM2 keep running even if the computer shuts down?
The server stops if the machine powers off. However, on remote servers, PM2 keeps processes alive regardless of terminal sessions ending.
What's the first command to memorize in PM2?
`pm2 start`, `pm2 list`, `pm2 logs`, and `pm2 restart` cover most needs. With these four, you can handle basic server operations.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: PM2로 Node 서버 상시 실행하는 방법과 기본 명령어