How to Keep Node.js Running at Linux Server

2014/8/22 min read
bookmark this
Responsive image

How to Use respawn + nuhup npm module to keep node.js running

 

nohup node respawn.js &

There's a good article about how to use respawn. http://java.dzone.com/articles/how-keep-nodejs-processes.

How to use respawn.

1. create a file under, any name the code could be reference -> https://github.com/mafintosh/respawn 2. that's it
3. type "node [your file name contains respawn code].js"

After you finish this, you site will not be down.
For example, if your /failUrl has syntax error, if start node server.js, your node.js app will down. However, with respawn /failUrl will be showing down. But page will load successfuly when you go to home page /.

Respawn is great, simple and easy to use. However didn't work for me because I'm using Command line to login to my linux box. So if i close my command line window, the thread is gone. So I have to let the respawn running at background. That's nohup.

The forever npm module is doing the same thing, however if you know nohup you can have other choice. I had a experience that, one day my node.js web application doesn't work, and type npm intall forever contains lots of error and take lots of time to figure out what's going on. But with nohup and respawn is easy.
The previous you just need to change the last 3 step to following.

nohup node respawn.js &


After close your remote console window, my linux node.js server still working fine.

 Node.js/Express.js is great for Web API’s and applications. In contrast to known enterprise technologies, Node.js is very special. It’s a single process/threaded environment. If an unhanded exception occurs, the Node.js virtual machine simply stops, leaving the application in an unresponsive state.

Due to the async nature of Node.js, try/catch doesn't always work, even with domains and stuff you have a chance that the application crashes on production while you sleep.

from http://java.dzone.com/articles/how-keep-nodejs-processes.

This's node.js, it's different.