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
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.
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.
| Keys | What it does |
|---|---|
Ctrl-a d | Detach: leave the session running and return to your normal shell |
Ctrl-a ? | Show the key bindings |
Ctrl-a k | Kill the current window (asks for confirmation) |
Ctrl-a \ | Quit: kill all windows and end the session |
Ctrl-a a | Send a real Ctrl-a to the program in the window |
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:
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
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.
| Command | Behaviour | Use it when |
|---|---|---|
screen -d -r name | Reattach, detaching it from the other terminal first if necessary | The old connection is dead or you've moved to a new computer |
screen -D -r name | As above, but also logs out the other terminal | You want to be sure the old login is closed |
screen -x name | Attach as well, without detaching the other terminal | You 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:
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
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.
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
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.
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.
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.
Chapter 2 Quick Reference
screenstarts a session;screen -S namenames it;echo $STYshows whether you're inside oneCtrl-ais the command key:ddetach,?help,kkill window,\quit,asend a literal Ctrl-ascreen -rreattaches (by name or ID if there are several)-d -rdetaches elsewhere then reattaches;-D -ralso logs out the other terminal;-xshares the sessionscreen -d -R -S namereattaches or creates;screen -D -Rmeans "attach here and now"screen -d namedetaches from outside;Ctrl-a :sessionname newrenames- A session ends when its last window closes — detach with
Ctrl-a dinstead of typingexit