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.

Read: Why Choose Node.js for Real-Time Application Development


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 holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply