Home

Keeping your Node Server running Forever

Created June 30, 2012

Yeah, it's really cool that you can start your node server from a terminal window by typing 'node server.js', but then you close the terminal or disconnect and your node server is no longer running... Well, wouldn't it be great if you were able to keep your Node servers running forever? That's exactly what you can do with the tool 'Forever'.

Forever is super easy to install:

$ [sudo] npm install forever

You may want to add this globally by adding '-g' at the end of that command.

So, after you have installed forever, you can simply get a Node JS server running forever by typing:

$ forever start server.js

And just like that your server.js Node will be running forever. You can easily list out all your servers that are running with the following command:

$ forever list
  0 server.js [ 24611, 24596 ]

The command will list the current index, server file, and the process ID's.

Then to stop the server you would simply enter:

$ forever stop 0
Forever stopped process:
  0 server.js [ 24611, 24596 ]

By passing in the index of the current running server, 'Forever' will stop that server.

And there you go! It's as simple as that. You can checkout the github repot for full documentation at: https://github.com/nodejitsu/forever/

Additionally for a great article about the Forever CLI tool, checkout NodeJitsu's article on keeping a node JS server up with forever.