Node应用实现系统重启自动启动与文件变更自动重启的最优工具及使用方法咨询
Hey there! Great question—combining these two critical features (boot-time startup and auto-restart on code changes) doesn’t require juggling multiple tools. PM2 is the all-in-one solution that checks every box here, and it’s the industry standard for Node.js process management. Let’s walk through exactly how to set it up.
Why PM2?
- Built-in
watchfunctionality to auto-restart your app when files change (no need for nodemon alongside it) - Keeps apps running in the background even after you close the terminal
- Automatically restarts apps if they crash
- Easy setup for system-wide boot-time startup
- Works across Linux, macOS, and Windows
Step-by-Step Setup
1. Install PM2
First, install PM2 globally on your system:
npm install pm2 -g
2. Start Your App with Auto-Restart on File Changes
Launch your Node.js app and enable the watch feature in one command:
pm2 start index.js --watch
- The
--watchflag tells PM2 to monitor all files in the current directory (and subdirectories) for changes. When you editindex.jsor any other file in the project, PM2 will instantly restart your app.
Optional: Use a Config File for Advanced Control
For more flexibility (like specifying which directories to watch, ignoring files/folders, or setting environment variables), create an ecosystem.config.js file in your project root:
module.exports = { apps: [ { name: "my-node-app", script: "./index.js", watch: true, // Ignore specific files/folders to avoid unnecessary restarts ignore_watch: ["node_modules", "logs", "*.log"], env: { NODE_ENV: "development" } } ] };
Then start your app using the config file:
pm2 start ecosystem.config.js
3. Set Up Boot-Time Auto-Start
PM2 makes it trivial to configure your app to start automatically when the system boots. Run this command:
pm2 startup
This will detect your operating system (systemd, upstart, launchd, etc.) and generate a command to set up the startup script. Follow the on-screen instructions—usually, you’ll need to run a second command like:
pm2 save
This saves your current PM2 process list, so those apps will be restored on boot.
4. Verify Everything Works
- Test file change restart: Edit
index.jsand runpm2 logsto see the restart log in real time. - Test boot-time startup: Reboot your system, then run
pm2 list—your app should be running without any manual intervention.
Alternative: Nodemon + PM2 (If You Prefer Nodemon)
If you’re already attached to Nodemon’s specific features (like more granular watch rules or custom restart messages), you can run Nodemon under PM2:
pm2 start nodemon --watch -- index.js
But honestly, PM2’s built-in watch feature is robust enough for most use cases, so this is rarely necessary.
Final Notes
PM2 is hands down the most efficient tool for this job—it eliminates the need for separate tools like forever or nodemon (unless you have very specific edge cases). It’s lightweight, well-documented, and widely used in production environments.
内容的提问来源于stack exchange,提问作者James

