1

I am using the following very very basic script in a .bat file to keep a node.js server running on a windows machine.

: loop
npm start
goto loop
: end

However, if the server goes down, it does not restart automatically.

I do know that there are preferable ways of keeping the node up and running (example), but I really want to focus on other parts of the code at the moment and keep on integrating with other partners there. Thus, I am really looking for a very very simple bat file that can restart the server when it goes down (on Windows). What can possibly be wrong with the above one that I have?

Community
  • 1
  • 1
MightyMouse
  • 13,208
  • 8
  • 33
  • 43
  • it might go wrong if the server goes *down* not as you think it does. There are many ways of application crashing, one of which makes application hang, thus the script never gets to the stage `goto loop`. Why not use `nodemon` module? – alandarev Sep 10 '14 at 11:38
  • Thank you. I am merely referring to crashing though. – MightyMouse Sep 10 '14 at 11:43

1 Answers1

2

Probably (you will have to check it), npm is a batch file (.bat or .cmd).

When you invoke a batch file from another batch file, the execution is transfered to the called batch and does not return to the caller. In your case, your goto loop is never reached as npm will never return

You need to use call npm start, so the execution will continue in the caller when the called batch ends.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This looks like it solves the problem. I can see two node instances running on the task manager, and when I terminate one of them it restarts as expected. Thank you for your time and the educated guess. :) – MightyMouse Sep 10 '14 at 11:46