Multiple Methods to Keep Processes Running After SSH Session Termination and Their Technical Principles

Nov 26, 2025 · Programming · 8 views · 7.8

Keywords: SSH | Process Management | Linux | disown | nohup | tmux

Abstract: This paper provides an in-depth analysis of technical solutions for maintaining remote process execution after SSH session termination. By examining the SIGHUP signal mechanism, it详细介绍介绍了disown command, nohup utility, and terminal multiplexers like tmux/screen. The article systematically explains the technical principles from three perspectives: process control, signal handling, and session management, with comprehensive code examples demonstrating practical implementation. Specific solutions and best practices are provided for different scenarios involving already running processes and newly created processes.

SSH Session and Process Termination Mechanism

In Linux environments, when users connect to remote servers via SSH and launch processes, these processes are typically associated with sessions and process groups. When the SSH client disconnects, the server-side sshd process sends SIGHUP (hangup) signals to all related child processes, which by default causes process termination.

disown Command Solution

For processes already running, job control combined with the disown command can be used to maintain execution. The specific operational steps are as follows:

# Assuming myprogram is running in foreground
# Press Ctrl+Z to suspend the process
[1]+  Stopped                 myprogram

# Use disown command to mark job to ignore SIGHUP signal
$ disown -h %1

# Resume process execution in background
$ bg 1
[1]+ myprogram &

# Now safely exit SSH session
$ logout

The -h option of the disown command is crucial—it doesn't remove the job from the job table but marks it so it won't be terminated when the shell receives SIGHUP. This method is particularly suitable for scenarios where processes have already started without prior consideration for persistence requirements.

nohup Utility Solution

For newly created processes, nohup (no hang up) is the most straightforward choice:

# Start long-running program using nohup
$ nohup ./long_running_script.sh &

# Standard output and error are redirected to nohup.out file
$ tail -f nohup.out

nohup works by making the executed command ignore SIGHUP signals and redirecting standard output to the nohup.out file. This method is simple and effective, especially suitable for batch processing tasks and scenarios requiring no interactive input.

Terminal Multiplexer Solution

Terminal multiplexers like tmux and screen provide more powerful session management capabilities:

# Start tmux session
$ tmux

# Run program within tmux session
$ ./my_program

# Detach session (Ctrl+b then d)
# Program continues running in background

# Restore session when reconnecting
$ tmux attach

The advantage of terminal multiplexers lies in maintaining complete terminal state, including output history, environment variables, etc. tmux, as a modern replacement for screen, offers richer features such as window splitting and session sharing.

Technical Principle Deep Analysis

From the operating system perspective, these methods primarily revolve around signal handling and process relationship management:

disown prevents signal propagation by modifying markers in the shell's internal job table, nohup sets signal handling before process startup, and terminal multiplexers isolate signal effects by creating independent pseudo-terminal sessions.

Practical Recommendations and Considerations

Based on different usage scenarios, the following strategies are recommended:

  1. Interactive Tasks: Prefer tmux for subsequent monitoring and interaction
  2. Batch Processing Tasks: Use nohup with output redirected to log files
  3. Emergency Situations: Apply disown solution to already running processes
  4. Production Environments: Consider using process management tools like systemd

Attention should be paid to output redirection issues, especially when processes need to output to terminals. For programs requiring continuous input, terminal multiplexers are the only viable option.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.