基于Meteor+Angular的网页遇意外错误,如何实现服务器崩溃自动重启?
Got it, let's work through getting your Meteor+Angular server to auto-restart when it crashes—super useful when you're stuck debugging that vague "Unexpected error" and need to keep your app online. Below are the most reliable approaches, depending on whether you're running in development or production:
1. Use PM2 (Recommended for Production & Development)
PM2 is the go-to process manager for Node.js apps (and Meteor runs on Node under the hood), and it handles auto-restarts out of the box. Here's how to set it up:
- Install PM2 globally:
npm install pm2 -g - For production environments:
First, build your Meteor app bundle:
Navigate to the bundle's server directory and install dependencies:meteor build ../your-app-bundle --server-only
Now start the app with PM2 (from thecd ../your-app-bundle/bundle/programs/server npm installbundledirectory):pm2 start main.js --name "meteor-angular-app" - For development environments:
You can even use PM2 to wrap your regular Meteor dev server, in case it crashes and doesn't auto-restart on its own:pm2 start meteor -- run --name "meteor-angular-dev" - Lock in auto-restart & persistence:
PM2 will automatically restart your app if it crashes, but to make sure it starts on server boot:pm2 startup pm2 save - Check logs (critical for debugging later):
Use this to see what's causing the crashes in the first place:pm2 logs meteor-angular-app
2. Systemd Service (For Production on Linux Servers)
If you're running on a Linux server and want a more native, system-level solution, use systemd. It'll handle auto-restarts and ensure your app runs on boot.
- Create a systemd service file:
Open a new file at/etc/systemd/system/meteor-angular.servicewith your editor of choice, then paste this (update the paths and environment variables to match your setup):[Unit] Description=Meteor Angular Application After=network.target mongod.service [Service] User=your-linux-username WorkingDirectory=/path/to/your/app/bundle ExecStart=/usr/bin/node main.js Restart=always RestartSec=10 Environment="PORT=3000" Environment="MONGO_URL=mongodb://localhost:27017/your-database-name" Environment="ROOT_URL=http://your-app-domain.com" [Install] WantedBy=multi-user.target - Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable meteor-angular.service sudo systemctl start meteor-angular.service - Check status and logs:
sudo systemctl status meteor-angular.service # Follow real-time logs journalctl -u meteor-angular.service -f
Quick Note for Development
Meteor's default development server (meteor run) usually auto-restarts on code changes and minor crashes. If it's not restarting on severe crashes, wrapping it in PM2 (as shown above) is a quick fix to keep your dev environment running while you debug.
Important Reminder
Auto-restart is a band-aid to keep your app online—make sure to dig into the logs from PM2 or systemd to track down that "Unexpected error" root cause. The logs will have stack traces or error messages that point you to the issue.
内容的提问来源于stack exchange,提问作者StormTrooper

