sunknudsen
sunknudsen

Reputation: 7260

How to automatically restart Node scripts when they crash in production?

The forever package appears stale (no commits in over 2 years) and its dependencies are out of date.

Upvotes: 4

Views: 12135

Answers (2)

dimitris tseggenes
dimitris tseggenes

Reputation: 3186

Your solution is PM2. It is a production process manager for Node applications, very easy to use. Installing PM2 with npm and running a simple command like pm2 start app.js you are almost done.

Additionally, you will have some useful tools, like logs and monitoring of your app status.

PM2 running application will be restarted automatically if the application crashes or is killed. Also, you can secure that your app will launch on server reboot, by adding it on system startup with pm2 startup systemd command.

Upvotes: 3

clovis1122
clovis1122

Reputation: 712

A great package for this is pm2, but I encourage you to make your application more resilient to failures since it will certainly come at the cost of some downtime.

If you want to recover/ignore an error, a try/catch at the top of the code should do it.

Upvotes: 0

Related Questions