How to Run a Node.js App as a Background Service ? - GeeksforGeeks (2024)

Last Updated : 14 Jun, 2024

Summarize

Comments

Improve

Running a Node.js application as a background service is a common requirement for ensuring that your app stays running even after you log out, or if the system restarts. This can be particularly important for server-side applications, APIs, or any long-running processes. This article explores several methods to run a Node.js app as a background service, including using systems on Linux, PM2 process manager, and creating a Windows service.

To run a node.js app as a background service is to even after closing the node terminal, the app server needs to be kept running.

Table of Content

  • Using forever
  • Using systemd on Linux
  • Using nohup

Using forever

The easiest method to make a Node.js app run as a background service is to use the forever tool. forever is a simple Command Line Interface (CLI) tool that ensures that a given script runs continuously without any interaction.

Installation:

npm install forever -g

How to Run a Node.js App as a Background Service ? - GeeksforGeeks (1)

Example of Installation of forever

Command to Start forever

To start the forever tool, run the following commands replacing <app_name> with the name of the node.js app.

forever start /<app_name>/index.js

How to Run a Node.js App as a Background Service ? - GeeksforGeeks (2)

Example of Starting forever

Using systemd on Linux

Another method involves creating a service file and manually starting the app and enabling the service to keep it running in the background. This method uses systemd, a system and service manager for Linux operating systems.

Step 1: Create a new file <app_name>.service file replacing <app_name> with the name of the node.js app. Inside the files, enter the following values

ExecStart=/var/www/<app_name>/app.js
Restart=always
User=nobody
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/<app_name>

Step 2: After configuring the service file,Copy the <app_name>.service file into the /etc/systemd/system.

Step3: Start the app with the following command to make it run with the service file:

systemctl start <app_name>

Using nohup

Another method to run a Node.js app as a background service is by using the nohup command. nohup is a Unix command that allows a process to continue running in the background after the user has logged out.

Run the following command to start the nohup replacing the <app_name> with the name of the node.js app:

nohup node /<app_name>/index.js &

The nohup command does not terminate this process even then the command terminal is closed.

How to Run a Node.js App as a Background Service ? - GeeksforGeeks (3)

Example of using nohup



Please Login to comment...

How to Run a Node.js App as a Background Service ? - GeeksforGeeks (2024)
Top Articles
Mistake of Fact Contributions
How much does a book cover design cost in 2023? - GetCovers
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6697

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.