How to run node.js server in background?


Running your node & express apps on your local machine is easy. However, once you put them in production there are some new problems.

What if the app crashes? What if the server restarts? What if some other failure happens? You need your apps to be running forever, no matter what happens.

Unfortunately applications crash in unexpected ways and servers restart due to bugs or power or hardware failures. In all cases, you need to ensure that your app can return to operating normally, all by itself.

Here in this post, we aim to help you setup up an Ubuntu server to run Node.js applications, including apps based on Express. These instructions will help you avoid some security mistakes, as well as provide some surprising benefits such as:
  1. You will not run your app as root; therefore, your app will be more secure.
  2. Your application will restart if it crashes, and it will keep a log of unhandled exceptions.
  3. our application will restart when the server starts - i.e. it will run as a service.
You can use the following npm to awake your server every time, no matters what happens, These tools will take care of the broken server.

1. forever npm

This is a simple CLI tool for ensuring that a given script runs continuously.
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 the parameter to restart automatically whenever your server.js file changes:
forever -w server.js

2. Run your app using PM2, and ensure that your node.js application starts automatically when your server restarts.

You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer. Install PM2
npm install pm2 -g
Start an application
pm2 start app.js
If you using express then you can start your app like
pm2 start ./bin/www --name="app"
Listing all running processes:
pm2 list
It will list all processes. You can then stop/restart your service by using the ID or Name of the app with the following command.
pm2 stop all                  
pm2 stop 0                    
pm2 restart all
To display logs
pm2 logs ['all'|app_name|app_id]

3. You could simply use "nohup"

You can use nohup and supervisor to make your node app run in the background even after you log out. This will keep the application running and to shut it down you will have to kill it. For that, you could install and then search htop for node and then kill it

For Linux Use terminal and enter in superuser mode, and try these code.
nohup node <location of of js file> &
Note: This '&' is just before you press enter or for npm command, just go to the location by cd command where your package.json is stored. Then
nohup npm start & exit
Note: This '&' is just before you press enter To stop it
You can see process id here, then use following code
kill -9 <PROCESS_ID>

Post a Comment

Post a Comment (0)