Detach & Reattach

GNU Screen & Linux Job Control

Chapter 2 ยท Starting, Detaching & Reattaching Sessions

Chapter 1 explained why screen keeps jobs alive. This chapter is the everyday workflow: start a session, leave it running, and come back to it — including what to do when the session is still attached somewhere else.

Starting a Session

# Start an unnamed session screen # Start a session with a meaningful name (recommended) screen -S backup # Start a session that runs one command directly screen -S import mysql mydb < dump.sql

A new session opens a fresh shell that looks exactly like a normal terminal. Nothing on screen tells you you're inside it, which is easy to forget. The -S option names the session, and the name shows up later when you list or reattach sessions (Chapter 3). If you give screen a command, the session ends when that command finishes.

Am I inside screen?
Run echo $STY. Inside a screen session it prints the session's ID, such as 12345.backup; outside, it prints nothing.

The Command Key: Ctrl-a

Screen listens for one special key combination, the command character, which is Ctrl-a by default. You press Ctrl-a, let go, and then press a second key to give screen a command. Everything else you type goes straight to the program in the window.

KeysWhat it does
Ctrl-a dDetach: leave the session running and return to your normal shell
Ctrl-a ?Show the key bindings
Ctrl-a kKill the current window (asks for confirmation)
Ctrl-a \Quit: kill all windows and end the session
Ctrl-a aSend a real Ctrl-a to the program in the window
Ctrl-a normally means something else
In the bash shell, Ctrl-a moves the cursor to the start of the line. Inside screen that keystroke is taken by screen, so use Ctrl-a a to get the old behaviour.

Detaching

Press Ctrl-a d. You're returned to the shell you started screen from, with a message showing which session you left:

[detached from 12345.backup]

Everything inside the session keeps running. You can now log out, or simply lose your connection — the session carries on either way. Detaching is deliberate; losing the connection is accidental; to screen they have the same effect.

Reattaching

# Reattach when there is only one detached session screen -r # Reattach a particular session by name or ID screen -r backup screen -r 12345

If there are several detached sessions, a plain screen -r doesn't guess: it lists them and asks you to choose one. This is where naming sessions with -S pays off.

When the Session Is Still Attached Somewhere Else

A common situation: your connection dropped, but the server hasn't noticed yet, so it still thinks you're attached. screen -r refuses, because the session isn't detached. You have three choices.

CommandBehaviourUse it when
screen -d -r nameReattach, detaching it from the other terminal first if necessaryThe old connection is dead or you've moved to a new computer
screen -D -r nameAs above, but also logs out the other terminalYou want to be sure the old login is closed
screen -x nameAttach as well, without detaching the other terminalYou want to share the session or watch it from two places (Chapter 9)

Reattach-or-Create

Screen can also create the session if it doesn't exist yet, which makes a convenient "take me to my work session" command:

# Reattach "work", detaching it elsewhere if needed, or create it screen -d -R -S work # "Attach here and now": reattach, logging out elsewhere if needed, or create screen -D -R
A handy alias
Adding alias work='screen -d -R -S work' to your ~/.bashrc gives you a single command that always drops you back into the same session.

Detaching From Outside, and Renaming

# Detach a session from another terminal, without attaching to it screen -d backup # Inside a session: rename it (press Ctrl-a, then type : to get a command prompt) Ctrl-a :sessionname nightly-backup

Ending a Session

A session ends when its last window closes. The usual way is simply to type exit in the shell (or let the command you started finish). Ctrl-a \ closes every window at once, after asking you to confirm.

Detach, don't exit
A very common mistake is typing exit when you meant to detach. That ends the shell and stops anything running in it. If you want to come back later, use Ctrl-a d.

Hands-On Exercises

Exercise 1

Start a session named counter and run while true; do date; sleep 5; done in it. Detach, wait a minute, reattach, and confirm the loop kept running while you were away. Then end the session cleanly.

๐Ÿ“„ View solution
Exercise 2

Open two terminals. Attach to one session from the first. In the second, try screen -r, then screen -x, then screen -d -r. Describe what happens in each terminal after each command.

๐Ÿ“„ View solution
Exercise 3

Write a ~/.bashrc alias or small function that takes you into a session called work, whether or not it exists and whether or not it's attached elsewhere. Explain which options you chose and why.

๐Ÿ“„ View solution

Chapter 2 Quick Reference

  • screen starts a session; screen -S name names it; echo $STY shows whether you're inside one
  • Ctrl-a is the command key: d detach, ? help, k kill window, \ quit, a send a literal Ctrl-a
  • screen -r reattaches (by name or ID if there are several)
  • -d -r detaches elsewhere then reattaches; -D -r also logs out the other terminal; -x shares the session
  • screen -d -R -S name reattaches or creates; screen -D -R means "attach here and now"
  • screen -d name detaches from outside; Ctrl-a :sessionname new renames
  • A session ends when its last window closes — detach with Ctrl-a d instead of typing exit