Joe
Joe

Reputation: 13091

NodeJS - how to make a process to auto restart when issue occurs?

I am running some application in NodeJS that retrieves data from a database and serve at port 3000.

It is running fine most of the time but sometimes it just errors out (it could be too many connections or network issue or some sql injection etc.). This happens every 2-3 days randomly.

While I am figuring out root cause - what is the best way to have NodeJS always 'ON' - meaning if it stops running - some process kicks it and it starts again?

Upvotes: 0

Views: 44

Answers (2)

Milan Velebit
Milan Velebit

Reputation: 2017

You can use forever as stated by the previous answer, but as I've worked with forever before, I'd suggest using the pm2 process manager. It has, on top of what you need, some more advanced functionalities and is well-documented.

Upvotes: 2

TheCleverIdiot
TheCleverIdiot

Reputation: 410

Try this: forever start -w <filename>

-w is for watch. it watches the file changes and restarts if any changes found.

Also, if the script is unexpectedly stopped, it will try to restart it.

forever list for details of the process.

you will see the PID, filename, log filename.

in the log file, you can see the log of your script.

Hope it helps. :)

Upvotes: 1

Related Questions