Keeping Node.js Processes Running in the Background
When deploying Node.js applications to a server, especially via SSH, you often need to ensure they continue running even after you disconnect from the session. This tutorial will cover several methods for achieving this, ranging from simple shell commands to more robust process management tools.
The Problem: Disconnecting and Process Termination
By default, when you close an SSH session, any processes you started within that session are typically terminated. This is because the session’s controlling terminal is gone, and the processes receive a SIGHUP (hangup) signal. For a long-running Node.js server, this is undesirable. We need to detach the process from the terminal and keep it running in the background.
Basic Backgrounding with &
The simplest way to start a process in the background is by appending the &
symbol to the command. For example:
node server.js &
This starts server.js
in the background, immediately returning control to your terminal. However, this alone isn’t enough. Closing the SSH session will still likely terminate the process.
Using nohup
The nohup
command is designed to prevent a process from receiving the SIGHUP signal. It stands for "no hangup." Combine it with the background operator (&
) and output redirection for a more reliable solution:
nohup node server.js > /dev/null 2>&1 &
Let’s break down what’s happening:
nohup node server.js
: This tellsnohup
to runnode server.js
and ignore the SIGHUP signal.> /dev/null
: This redirects standard output (stdout) to/dev/null
, effectively discarding it.2>&1
: This redirects standard error (stderr) to the same location as standard output (stdout), also discarding it. This is important to prevent error messages from being written to your terminal.&
: This puts the entire command in the background.
This method is a significant improvement, but the process can still be susceptible to system-level terminations. Also, without proper logging, debugging can be challenging.
Understanding Output Redirection
The >
and 2>&1
parts of the nohup
command are crucial for managing output.
>
redirects stdout.node server.js > output.log
would write the output of the script tooutput.log
.2>&1
redirects stderr to the same file descriptor as stdout. This means error messages and regular output will be combined. If you redirect stdout to/dev/null
, then errors are also discarded. For proper logging, redirect to a file instead of/dev/null
.
Using disown
The disown
command removes a process from the shell’s job table. This prevents the shell from sending signals to it when the shell exits.
nohup node server.js &
disown -h %1
Here’s what’s happening:
nohup node server.js &
starts the process in the background withnohup
to ignore hangup signals.disown -h %1
removes the first background process (identified by%1
) from the shell’s job control. The-h
flag prevents SIGHUP from being sent to the process.
More Robust Solutions: Process Managers
While nohup
and disown
can work for simple cases, dedicated process managers offer more features like automatic restarts, logging, and monitoring.
-
PM2: A popular and feature-rich process manager specifically designed for Node.js applications. It simplifies deployment and management.
npm install -g pm2 pm2 start server.js pm2 startup [platform] # generate startup script for your OS
PM2 provides features like load balancing, monitoring, and automatic restarts in case of crashes.
-
forever: Another Node.js process manager. It’s simpler than PM2 but still provides essential features.
npm install -g forever forever start server.js
Using screen
or tmux
screen
and tmux
are terminal multiplexers. They allow you to create persistent terminal sessions that can be detached and reattached. This is an excellent solution if you need to interact with your application’s console.
screen # or tmux
node server.js
# Detach with Ctrl+a+d (screen) or Ctrl+b+d (tmux)
# Reattach with screen -r or tmux attach
Choosing the Right Approach
- For very simple cases where you just want to run a script in the background and don’t care about monitoring or restarts,
nohup
anddisown
can be sufficient. - For production environments and more complex applications, a process manager like PM2 or forever is highly recommended. They provide the reliability, monitoring, and features you need to keep your application running smoothly.
- If you need to interact with the application’s console,
screen
ortmux
are excellent choices.