43

We are finishing development of a project, the client is already using it but occasionally some errors occur - crashing the server.

I know I could register a service as 'upstart' script on linux, in order to have my node service restart when it crashes.

But our server is running other stuff, so we can't restart it. Well, actually, while writing, I realize I have two questions then:

  • Will 'upstart' work without having to reboot? Something is just whispering yes to me :)
  • If not, what other option would I have to 'respawn' my node server when it crashes?

4 Answers 4

48

Yes, upstart will restart your process without a reboot.

Also, you should look into forever.

2
  • 5
    The Global Error Handler is discouraged by joyent's guide to error handling: joyent.com/developers/node/design/errors. In case some unknown bug caused an exception, it's better to crash the app (and restart it) than leave it in a bad state that will lead to more hard-to-debug bugs.
    – syonip
    Mar 21, 2015 at 15:37
  • 1
    True. I wasn't aware of that when I posted this answer. Thanks for pointing it out. I'll remove that.
    – Daniel
    Mar 23, 2015 at 6:24
16

PM2 is a Production process manager for Node.js app.

0
5

If your focus for automatic restart is an always running application, I suggest to use a process manager. Process manager, in general, handles the node process(es if cluster enabled), and is responsible for the process/es execution. PM leans on the operative system: your node app and the OS are not so strinctly chained because the pm is in the middle.

Final trick: put the process manager on upstart.

Here is a complete performance improvement path to follow.

0

Using a shared server and not having root privileges, I can't download or install any of the previously mentioned libraries. What I am able to do is use a simple infinite bash loop to solve my issue. First, I created the file ./startup.sh in the base directory ($ vim startup.sh):

#!/bin/bash
while:
do
    node ./dist/sophisticatedPrimate/server/main.js
done

Then I run it with:

$ bash startup.sh

and it works fine. There is a downside to this, which is that is doesn't have a graceful way to end the loop (at least not once I exit the server). What I ended up doing is simply finding the process with:

$ ps aux | grep startup.sh

Then killing it with

$ kill <process id>
example
$ kill 555555

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.