run nodejs app in background

How to Run NodeJS App in Background

NodeJS is a popular web development framework to to build javascript-based applications. However, when you run a NodeJS app, it runs in foreground and does not allow you to run other processes while it is running. Sometimes you may need to run a NodeJS App or server in background so that you can do other things. In this article, we will look at the different ways to run NodeJS app in background.


How to Run NodeJS App in Background

Here are the different ways to run NodeJS app in background. For our example, we will assume that we have a NodeJS server app.js at /home/ubuntu/app.js. Please update the file path of NodeJS file in all the following commands, as per your requirement.


1. Using Forever

Forever is a simple NodeJS package that allows you to run NodeJS applications as a process. Here is the command to install it.

$ sudo npm install forever

Here is the command to start a NodeJS app using forever. If you do not provide absolute path to your .js file, forever will look for it in your present working directory.

$ sudo forever start /home/ubuntu/app.js

Here is the command to stop NodeJS app.

$ sudo forever stop /home/ubuntu/app.js

By default, forever uses node and you don’t need to specify it. But if you use older versions of NodeJS, you may need to use –harmony option along with node to run the app. In this case, we also need to use -c option with forever to explictly run a NodeJS command within quotes.

$ sudo forever start -c "node --harmony app.js"


2. Using nohup &

You may also use the nohup command along with & to run NodeJS app in background, since they allow you to run any Linux process in background. Here is an example to run app.js in background.

$ nohup node /home/ubuntu/app.js &

Although this is a common way to run processes in background, it does not give you a good control over the process. For example, you cannot stop or restart the process easily. You will need to find out the PID of this process and then use kill command to stop it, which is not a graceful exit.


3. Using systemd

Most Linux systems have systemd that allows you to define & manage services. In this case, you need to add the following command to the first line of your app.js file. It will set the runtime environment for your application.

#!/usr/bin/env node

Next, make this file an executable with the following command.

$ sudo chmod +x /home/ubuntu/app.js

Create a service file for your app at /etc/systemd/system

$ sudo vi /etc/systemd/system/app.service

Add the following lines to it. Specify the absolute path to your NodeJS file for ExecStart variable. Also update WorkingDirectory to be the directory that contains your app.js file.

[Unit]
Description=App

[Service]
ExecStart=/home/ubuntu/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/ubuntu

[Install]
WantedBy=multi-user.target

You can start your app with the following command.

$ sudo systemctl start app

You can also enable it to start on boot with the following command.

$ sudo systemctl enable app

You can view its logs using the following command

journalctl -u app

In this article, we have covered 3 different ways to run a NodeJS app as a background process. If you are looking for a simple way to run a background NodeJS app, then go for Forever. But if you need a more robust service, then try systemd.

Also read:

How to Fix EADDRINUSE error in NodeJS
How to Prevent Direct Access to PHP files
How to Fix NGINX Upstream Timed Out Error
How to Download File from NodeJS
How to Use ES6 Import in NodeJS

Leave a Reply

Your email address will not be published.