nohup and screen

You start a long process, close your laptop, and... it dies. SSH disconnects, and your 6-hour job terminates.

Let's fix that.

The Problem

When you log out or disconnect:

  1. Your shell receives SIGHUP
  2. Shell sends SIGHUP to all its children
  3. Children terminate

This is why closing your terminal kills your processes.

nohup - Ignore Hangup

Terminal
$nohup long_running_script.sh &
nohup: ignoring input and appending output to 'nohup.out'

nohup makes the process immune to SIGHUP. It keeps running when you log out.

Where Does Output Go?

Terminal
$nohup command &
$# Output goes to nohup.out
$
$# Or redirect it:
$nohup command > output.log 2>&1 &

disown - Detach Existing Process

Forgot nohup? Already started the process?

Terminal
$long_running_command
$# Oops, should have used nohup. Press Ctrl+Z
^Z [1]+ Stopped
$bg
[1]+ long_running_command &
$disown
$# Now safe to log out

disown removes the job from the shell's job table.

screen - Terminal Multiplexer

screen creates persistent terminal sessions you can detach from and reattach later.

Basic Usage

Terminal
$screen
(new screen session starts)
$# Run your long command
$# Press Ctrl+A then D to detach
[detached]
$# Now you can log out safely

Reattaching

Terminal
$screen -r
(reattach to session)

Multiple Sessions

Terminal
$screen -ls
There are screens on: 12345.pts-0.server (Detached) 12346.pts-1.server (Detached)
$screen -r 12345
(attach to specific session)

Named Sessions

Terminal
$screen -S deployment
(create named session)
$# ... work, then Ctrl+A D to detach
$screen -r deployment
(reattach by name)

screen Keyboard

All screen commands start with Ctrl+A:

  • Ctrl+A D = Detach
  • Ctrl+A C = New window
  • Ctrl+A N = Next window
  • Ctrl+A P = Previous window
  • Ctrl+A K = Kill window

tmux - Modern Alternative

tmux is like screen but more powerful and is the modern standard:

Terminal
$tmux
(starts tmux session)
$# Press Ctrl+B then D to detach
$tmux attach
(reattach)

tmux Key Commands

All tmux commands start with Ctrl+B:

  • Ctrl+B D = Detach
  • Ctrl+B C = New window
  • Ctrl+B N = Next window
  • Ctrl+B % = Split pane vertically
  • Ctrl+B " = Split pane horizontally
  • Ctrl+B Arrow = Switch between panes

Named Sessions

hljs bash
tmux new -s deploy    # Create named session
tmux attach -t deploy # Attach by name
tmux ls               # List sessions
tmux kill-session -t deploy  # Kill session

tmux is the Standard

Most DevOps professionals use tmux over screen. It has better pane splitting, more intuitive defaults, and excellent plugin support. Learn tmux - you'll use it daily.

When to Use Which

ToolUse Case
nohupSimple, one-off commands
disownAlready running, forgot nohup
screenInteractive work, need to return
tmuxSame as screen, more features
Knowledge Check

What happens when you SSH to a server, run a command, and close your laptop?

Quick Reference

CommandEffect
nohup cmd &Run immune to hangup
disownDetach current job from shell
screenStart multiplexer session
Ctrl+A DDetach from screen
screen -rReattach to screen
tmuxStart tmux session
Ctrl+B DDetach from tmux

Key Takeaways

  • Processes die when their parent shell dies (SIGHUP)
  • nohup makes processes immune to hangup
  • disown detaches already-running processes
  • screen/tmux create persistent sessions
  • Use these for any long-running remote work

Congratulations! You've completed Chapter 7: Process Management.

Next chapter: System Information - checking disk, memory, and CPU.