Search code examples
javascriptnode.jsforever

Have a node process automatically restart if a particular log response is encountered


I have a very simple node script which acts as an autoresponder for a messaging platform. I run the script in the background with forever, which will restart the script if it encounters a critical error. However, sometimes the script starts to fail because of a credential error likely caused by a server timeout of some kind. The script will keep running because the error is not fatal to the process, but the script is no longer working. The simplest, if not particularly elegant, thing to do would just be to restart the process from scratch.

Is there a simple way to have forever force a restart when this particular log message is encountered without rewriting the script itself? I know its probably better to rewrite error handling in the script, but this isn't mission critical and a kluge would be more or less fine.


Solution

  • You could use a combination of cluster module and forever module. Whenever you encounter a certain log response, you can force the program to exit and the forever module would restart that worker for you automatically.