How to keep a nodejs server running permanently

Sometimes you need to run your nodejs server permanently (.e.g. for building a REST API that will be consumed by a mobile application) without the need to runthe calling command manually every time you want to start the server. In this short tutorial you will learn the steps required to pull this off.

1 – Using nuhup

Nohup which stands for no hang up, is a Linux command that is used to keep processes active (running) even when the calling agent exits the shell or the terminal. You may want to visit this page for more information.

Now you would need to run the command below :

nohup node server.js > output.log &

This will make sure the server will keep running for ever.

2 – Using forever

The command forever is a CLI utility that is used to ensure that a given program or a script runs continuously. For more on forever command, you may want to visit this page.

Read: How to install Node.js on Linux/Ubuntu

Run the command below in order to install this tool and run node js server :

npm install forever $ forever start server.js          [nodejs forever]

The list of running servers can be checked using the option list as shown below:

forever list

Forever has the ability to restore your server even if it dies.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus has a degree in software development. He is passionate about gadgets with a screen, nostalgic for phones, a retired gamer and open source programmer. He likes also to write about macOS and Windows. design web pages and debug long programs!

Leave a Reply