使用Forever重启Node.js应用遇异常退出未重启的参数配置问题
Got it, let's break this down. The problem you're facing is that Forever's default logic treats quick, repeated exits (like your app crashing immediately on start) as an intentional failure, so it stops trying to restart after a few attempts. Since you don't want to fix the app itself and just need it to keep looping, you're missing two key parameters to override this behavior.
Here's what you need to do:
- Add
--minUptime <milliseconds>: This sets the minimum time your app needs to stay running before Forever considers it a "successful" start. If it exits before this threshold, Forever normally marks it as a spin failure. - Add
--spinSleepTime <milliseconds>: This defines how long Forever waits between restart attempts when your app fails to meet the--minUptimethreshold.
Putting it all together, your updated command would look like this:
forever start --minUptime 1000 --spinSleepTime 1000 your-app.js
What these parameters do specifically:
--minUptime 1000: Tells Forever that if your app runs for at least 1 second, it's a valid start. If it crashes faster than that, it's considered a "spin" failure.--spinSleepTime 1000: Instead of giving up after a few spin failures, Forever will wait 1 second and try restarting again—indefinitely.
If you want to remove any upper limit on restart attempts entirely, you can also add --max-restarts -1 (setting it to -1 disables the restart limit):
forever start --minUptime 1000 --spinSleepTime 1000 --max-restarts -1 your-app.js
This combination ensures that no matter how quickly your app crashes, Forever will keep trying to restart it on a fixed interval. I've used this exact setup for testing flaky apps where I needed continuous restarts without fixing the underlying issue.
内容的提问来源于stack exchange,提问作者Tobias Gassmann

