Why Does nohup Keep a Process Running After the Terminal Closes?
This article explains how Linux signals like SIGHUP affect background processes, why using nohup (or similar tools) prevents termination when the controlling terminal exits, and provides practical examples and code for handling signals directly.
When a command is started in a terminal and the terminal is closed, the operating system sends a SIGHUP (signal hang up) to the process, which by default terminates it. This behavior often surprises users who expect the process to continue running.
Background and Real‑World Example
The author recounts a personal experience configuring a Linux authentication client on a Netgear router. After SSHing into the router and starting the client, the network connection dropped as soon as the SSH session ended because the authentication program received SIGHUP and stopped.
Common Solutions
Append & to run the command in the background.
Use nohup to ignore SIGHUP: $ nohup python process.py & (outputs a job ID and creates nohup.out).
Alternative tools: screen, tmux, or setsid.
What nohup Does
The nohup utility invokes a command with its arguments and sets the SIGHUP signal to be ignored.
SIGHUP is sent to a process when its controlling terminal is closed on POSIX‑compatible systems (Unix, Linux, BSD, macOS). Ignoring this signal allows the process to keep running after the terminal disappears.
Understanding Signals
Signals are a form of inter‑process communication. A process can handle a signal in three ways: use the default action, install a custom handler, or ignore it (some signals cannot be ignored). For SIGHUP, the default action is termination.
The shell typically registers a handler that forwards SIGHUP to all its child processes before exiting. Programs like nohup explicitly set the handler to SIG_IGN so they survive.
Programming Example
#include <signal.h>
#include <unistd.h>
int main() {
signal(SIGHUP, SIG_IGN);
sleep(1000);
return 0;
}Compiling and running this program shows that it continues even after the terminal is closed.
Other Signal Uses
Beyond SIGHUP, other signals include:
SIGINT (Ctrl+C)
SIGKILL (cannot be ignored)
SIGTERM (graceful termination)
Custom signals SIGUSR1, SIGUSR2, SIGURG for application‑specific purposes.
For example, services like Apache or Nginx use SIGHUP to reload configuration without stopping.
Zombie Processes
If a child process exits but its parent does not call wait(), the kernel keeps a task_struct for the child, marking it as a zombie (state Z). Zombies appear in ps or top and cannot be killed until the parent reaps them.
Understanding SIGHUP and proper signal handling prevents unintended zombie processes and ensures background tasks run reliably.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
