Home / Alt manpages / screen(1)

  • screen(1)
  • User command
  • linux

Keep a Shell Session Alive with GNU screen

Your SSH connection drops mid build, and every process in that terminal dies with it, unless you started the job inside GNU screen. This guide gets you a named session that survives a dropped connection, plus the handful of key presses you actually need for detaching, reattaching and running more than one window at once. The examples match GNU Screen 4.09.01, from the installed package that reports its manpage version as GNU Screen 4.9.1.

Allow about ten minutes. You need a terminal and a shell. Every command here is unprivileged: you do not need sudo, and nothing touches system services or global configuration.

1. Check what you have installed

Confirm 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

screen begins one session with one window and a shell, unless you tell it to run something else. Each window keeps its own process, so a program you started keeps running whether or not that window is visible, and whether or not the whole session is detached.

Every screen command starts with the prefix Ctrl-a, written as C-a below: press the control key and a, release both, then press the second key. Forget a shortcut and C-a ? lists the bindings from inside the session, no manual required.

2. Start a named session

Use -S to name a new session. A name like build beats trying to remember a generated terminal-and-host suffix later:

$ screen -S build
[inside screen]
$ printf 'session is running\n'
session is running

The prompt now belongs to the screen window. What you see when a session starts depends on your shell and terminal, so the check that matters is the command you actually run inside it.

Checkpoint

From inside the session, press C-a then d. screen detaches and drops you back at the outer shell. Detaching does not stop the shell or anything it started.

[detached from 12345.build]
$ screen -ls
There is a screen on:
        12345.build     (Detached)
1 Socket in /run/screen/S-example.

The process number, the socket directory and the exact wording will differ on your machine. Look for your named session and the word Detached, not a byte-for-byte match with this output.

3. Reattach without guessing which session

List sessions first whenever 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 short name like build works when it is unambiguous; fall back to the full pid.tty.host value from screen -ls when it is not. Press C-a then d again once you are back inside, to leave it running.

If the session is already attached somewhere else, plain -r refuses to take it over. That refusal is deliberate: it stops you splitting one session across two terminals by accident.

4. Take an attached session back deliberately

When you know the old terminal is gone for good, combine detach and reattach in one step:

$ screen -d -r build
  • -d -r detaches the session elsewhere and reattaches it here. This can disrupt whoever currently has the controlling display, so run screen -ls first and only use it when that interruption is intended.
  • -D -r is the stronger version. It detaches and logs out the remote display if needed. Treat it as service-disrupting, not a routine retry. To just inspect what exists, stop at screen -ls.
  • -R and -RR can create a brand new session when no suitable detached one exists. That convenience is also a trap: a typo in the session name, or an empty session list, can drop you into a new session instead of the one you meant. Prefer explicit -r NAME after listing sessions first.

5. Run more than one window

Press C-a c inside screen to open a new window with its own shell and switch straight to it. The original shell keeps running 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; the output includes the current-window marker and titles, and both can vary with your shell. The interactive shortcuts are faster day to day:

  • Next window: C-a n or C-a Space.
  • Previous window: C-a p.
  • Jump to a numbered window: C-a 0 through C-a 9.
  • Rename the current window: C-a A.
  • Toggle back to the last window you were on: C-a C-a.

Run screen command arguments from inside a window and screen normally opens another window in the same session, rather than nesting a second screen inside itself. It knows which session is its parent through the STY environment variable.

6. Fire off a long-running job safely

For a job you want detached from the start, launch screen with -dmS, a name and the command. It starts detached, so your current terminal is never taken over:

$ 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 above is harmless and finishes in 30 seconds. For a real job, quote the whole shell script as one argument to sh -c, and check its expected lifetime before you rely on this pattern for a deployment, backup or migration.

Reattach any time to check on it:

$ screen -r nightly-watch
Sat Sep 26 12:00:00 BST 2026
Sat Sep 26 12:00:30 BST 2026

Warning

Do not reach for C-a k or C-a \ out of habit. The first kills the current window; the second kills every window and ends screen entirely. If a job is still doing useful work, detach with C-a d instead. Want to stop the sample job early? Reattach and let it finish, or exit its shell the normal way.

7. Handle scrollback and logs deliberately

screen keeps scrollback history for each virtual terminal. Press C-a [ to enter copy mode, use your terminal's usual movement keys to scroll through it, then Esc to leave. Press C-a H to toggle logging for the current window; the default log file is screenlog.0, so avoid turning it on inside a directory holding secrets or personal data.

Want more history for a new session? Set it with -h and a line count:

$ screen -h 5000 -S inspect

That resizes the history buffer going forward. It does not retroactively recover old output, and it does not create a permanent log on its own.

Done means

  • screen -ls shows the named session you expect.
  • You can detach with C-a d and reattach with screen -r NAME.
  • You can open and switch between more than one window without stopping the others.
  • You check for an attached session before reaching for -d -r or the stronger -D forms.
  • You detach useful work instead of killing its window or the whole session.