How to Run a Node.js Web Application as a Linux Daemon
This article explains how to run a Node.js web application as a Linux daemon, covering foreground/background jobs, SIGHUP handling, disown, nohup, terminal multiplexers like screen and tmux, and dedicated tools such as forever, nodemon, pm2, and systemd for reliable service management.
A daemon is a process that runs continuously in the background. This guide shows how to start a web application as a daemon.
1. Problem Origin
After writing a web app, you need to launch it so it stays running. A simple Node.js server (server.js) with six lines looks like this:
var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World'); }).listen(5000);
Running it with $ node server.js works, but when you close the terminal the process exits.
2. Foreground vs. Background Jobs
A foreground job occupies the terminal. Adding & at the end of a command makes it a background job. You can also suspend a foreground job with Ctrl+Z and resume it with bg.
Background jobs inherit stdout and stderr but not stdin.
3. SIGHUP Signal
When a session ends, the shell may send a SIGHUP signal to its child processes, causing them to exit. The huponexit shell option controls this. Check its value with:
$ shopt | grep huponexit
On most Linux systems it is off, so background jobs usually survive session termination.
4. The disown Command
To ensure a background job is not affected by SIGHUP, use disown to remove it from the job table:
$ disown # remove the most recent background job
$ disown -r # remove all background jobs
$ disown -a # remove all jobs but keep them alive
$ disown -h # keep jobs but prevent SIGHUP
$ disown %2 # remove job with ID 2
After disowning, jobs will no longer list the process.
5. Standard I/O Redirection
If a background task still interacts with standard I/O, it will terminate when the session ends. Redirect its output:
$ node server.js > stdout.txt 2> stderr.txt
This prevents the process from hanging.
6. The nohup Command nohup makes a process ignore SIGHUP, closes stdin, and redirects output to nohup.out:
$ nohup node server.js
Combine with & to run it in the background.
7. Terminal Multiplexers: Screen and Tmux
Screen and Tmux let you create detachable sessions. Example with Screen:
$ screen
$ node server.js
Detach with Ctrl+A D, later reattach with $ screen -r. Use -S name to name sessions.
Tmux usage:
$ tmux
$ node server.js
$ tmux detach
$ tmux attach
Both tools allow you to keep services running after logout.
8. Node Process Managers
forever keeps a Node process alive and can restart it:
$ forever server.js
$ forever start app.js
$ forever stop Id
$ forever restart Id
$ forever -w server.js # watch files
$ forever -m 5 server.js # max restarts
$ forever list
nodemon watches files during development and restarts on changes:
$ nodemon server.js
$ nodemon --watch app --watch libs server.js
pm2 offers advanced process management, clustering, logging, and monitoring:
$ pm2 start app.js
$ pm2 start app.js -i max # cluster mode
$ pm2 list
$ pm2 stop 0
$ pm2 restart 0
$ pm2 delete 0
$ pm2 save
$ pm2 monit
$ pm2 logs
$ pm2 dump
$ pm2 kill
$ pm2 resurect
$ pm2 web # web UI at http://localhost:9615
9. Systemd
Beyond third‑party tools, Linux provides systemd as a native daemon manager. It integrates directly with the kernel, offering high performance and powerful features for managing services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
