How to make a node.js application run permanently?

Sam picture Sam · Oct 3, 2012 · Viewed 276.7k times · Source

On a Debian server, I installed Node.js. I understand how to launch an app from putty with this command line:

node /srv/www/MyUserAccount/server/server.js

and get to it on the address 50.51.52.53:8080 (IP and port).

But as soon as I close putty, then I cannot reach the address 50.51.52.53:8080 anymore.

How to make a Node.js application run permanently?

As you can guess, I am a beginner with Linux and Node.js.

Answer

N20 picture N20 · Oct 18, 2012

You could install forever using npm like this:

sudo npm install -g forever

And then start your application with:

forever server.js

Or as a service:

forever start server.js

Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:

forever -m5 server.js

To list all running processes:

forever list

Note the integer in the brackets and use it as following to stop a process:

forever stop 0

Restarting a running process goes:

forever restart 0

If you're working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:

forever -w server.js