Keep Long-Running Shell Work Alive with GNU screen
You will finish with a named GNU screen session that survives an SSH disconnect, plus a small set of commands for detaching, reattaching and switching between shell windows. The examples match GNU Screen 4.09.01, from the installed package that reports the manpage version as GNU Screen 4.9.1.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a terminal and a shell. The normal commands are unprivileged. You do not need sudo, and this guide does not alter system services or global configuration.
1. Check the installed command
Start by confirming which executable will run and which version it reports:
$ command -v screen
/usr/bin/screen
$ screen --version
Screen version 4.09.01 (GNU) 20-Aug-23
The command begins one session with one window and a shell, unless you give it a command to run. Each window has its own process, so a running program continues while another window is visible or while the session is detached.
Screen's command prefix is Ctrl-a, written as C-a below. Press the control key and a, release both, then press the second key. For example, C-a ? displays the bindings inside Screen. This is a useful escape hatch if you forget a shortcut.
2. Start a named session
Use -S to give a new session a name. A name is easier to recognise than a generated terminal and host suffix:
$ screen -S build
[inside screen]
$ printf 'session is running\n'
session is running
The shell prompt now belongs to the Screen window. The text shown when a session starts varies with your shell and terminal, so the useful check is the command you run inside it.
Checkpoint: from inside the session, press C-a then d. Screen should detach and return you to the outer shell. Detaching does not stop the shell or its child processes.
[detached from 12345.build]
$ screen -ls
There is a screen on:
12345.build (Detached)
1 Socket in /run/screen/S-example.
The number, user-specific socket directory and wording vary. Look for your named session and the word Detached, not an exact copy of this output.
3. Reattach without guessing
List sessions before attaching when more than one might exist:
$ screen -ls
There is a screen on:
12345.build (Detached)
1 Socket in /run/screen/S-example.
$ screen -r build
screen -r resumes a detached session. A name such as build is enough when it is unambiguous; use the full pid.tty.host value from screen -ls if necessary. When you are back inside, press C-a then d again to leave it running.
If the session is attached to another terminal, ordinary -r refuses to take it over. That refusal protects an active session from an accidental split-brain terminal.
4. Recover an attached session deliberately
When you know the old terminal is gone, use a detach-and-reattach combination:
$ screen -d -r build
-d -r detaches the session elsewhere and reattaches it here. This can disrupt whoever currently has the controlling display, so check screen -ls first and use it only when that interruption is intended.
-D -r is stronger: it detaches and logs out the remote display if needed. Treat it as a service-disrupting operation, not a routine retry. If you only want to inspect what exists, stop at screen -ls.
The -R and -RR forms can create a session when no suitable detached session exists. That convenience is also a trap: a typo in a session name or an empty session list can leave you in a new session instead of the one you expected. Prefer explicit -r NAME after listing sessions.
5. Add a second window
Inside Screen, press C-a c to create a new window with a shell and switch to it. The original shell remains alive in the first window.
$ printf 'window two\n'
window two
$ screen -Q windows
0$ bash 1$ bash
The screen -Q windows query asks the running session for its window list. Output includes the current window marker and titles, which can differ with your shell. The interactive shortcuts are usually quicker:
C-a norC-a Spacemoves to the next window.C-a pmoves to the previous window.C-a 0throughC-a 9selects a numbered window.C-a Alets you name the current window.C-a C-atoggles to the previously displayed window.
From inside a Screen window, running screen command arguments normally asks the existing session to create another window rather than nesting a second Screen. The STY environment variable is how Screen identifies that parent session.
6. Send a long-running command safely
For a detached job, start Screen with -dmS, a name and the command. This creates the session detached, so it does not take over your current terminal:
$ screen -dmS nightly-watch sh -c 'date; sleep 30; date'
$ screen -ls
There is a screen on:
23456.nightly-watch (Detached)
1 Socket in /run/screen/S-example.
The example is harmless and exits after 30 seconds. For a real command, quote the complete shell script as one argument to sh -c. Verify that the command has the expected lifetime before using this pattern for a deployment, backup or migration.
Reattach to inspect it:
$ screen -r nightly-watch
Sat Sep 26 12:00:00 BST 2026
Sat Sep 26 12:00:30 BST 2026
Do not use C-a k or C-a \ casually. The first destroys the current window; the second kills all windows and terminates Screen. If a command is still doing useful work, detach with C-a d instead. If you started the sample job and want to stop it early, reattach and let the command finish, or exit its shell in the normal way.
7. Handle logs and scrollback consciously
Screen keeps a scrollback history for each virtual terminal. Press C-a [ to enter copy mode, use the terminal's movement keys to inspect history, and press Esc to leave it. Press C-a H to toggle logging for the current window. The default log file is screenlog.0; avoid enabling it in a directory containing secrets or personal data.
For more history when creating a session, use -h with a line count:
$ screen -h 5000 -S inspect
This changes the size of the history buffer for the session. It does not make old output appear retroactively, and it does not create a permanent log.
Done means
screen -lsshows the named session you expect.- You can detach with
C-a dand reattach withscreen -r NAME. - You can create and select more than one window without stopping the others.
- You check attached sessions before using
-d -ror the stronger-Dforms. - You detach useful work instead of killing its window or the whole session.