How to prevent a background process from being stopped after closing SSH client in Linux?

A background process in Linux is a process that runs independently of the shell session. When you start a background process and then close your SSH client, the process typically gets terminated due to the SIGHUP signal (hangup signal) sent to all child processes. This article explains several methods to prevent background processes from being stopped after SSH disconnection.

Starting Background Processes

In Linux, you can start a process in the background by appending the & symbol after the command:

sleep 10000 &

This command runs the sleep process in the background for 10,000 seconds. The output shows the job number and process ID:

[1] 24101

You can check running background jobs using:

jobs
[1] running sleep 10000

Methods to Prevent Process Termination

1. Using nohup Command

The nohup (no hang up) command is the most widely used approach. It executes a command while ignoring the SIGHUP signal, ensuring the process continues running even after SSH disconnection:

nohup command_name &

For processes that should run at lower priority, combine nohup with nice:

nohup nice command_name &

The nice command reduces the process priority, allowing other processes to use more CPU resources.

2. Using GNU Screen

GNU Screen creates persistent terminal sessions that survive SSH disconnections. Install and use it as follows:

# Start a new screen session
screen -S session_name

# Detach from session (Ctrl+A, then D)
# Reattach to session
screen -r session_name

3. Using tmux

tmux is a modern alternative to GNU Screen with similar functionality:

# Start new tmux session
tmux new-session -d -s session_name

# Attach to session
tmux attach-session -t session_name

4. Double Fork Method

The double fork technique creates a process that becomes orphaned and gets adopted by the init process:

((command_name &)&)

Comparison of Methods

Method Use Case Advantages Disadvantages
nohup Simple background tasks Easy to use, lightweight No interaction after start
GNU Screen Interactive sessions Full terminal session, reconnectable Learning curve required
tmux Advanced terminal management Modern features, split windows More complex than nohup
Double Fork Daemon-like processes True background execution No process control

Common Use Cases

  • Web servers (Nginx, Apache) − Use nohup or systemd services

  • Long-running scripts − Use nohup with output redirection

  • Development work − Use tmux or screen for interactive sessions

  • System monitoring − Use double fork or dedicated service managers

Conclusion

Preventing background processes from terminating after SSH disconnection is essential for server administration. The nohup command is ideal for simple tasks, while tmux or screen provide full session management for interactive work. Choose the method that best fits your specific use case and workflow requirements.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements