LinuxCommandLibrary

screen

Manage detached, persistent terminal sessions

TLDR

Start a new screen session

$ screen
copy

Start a new named screen session
$ screen -S [session_name]
copy

Start a new daemon and log the output to screenlog.x
$ screen -dmLS [session_name] [command]
copy

Show open screen sessions
$ screen -ls
copy

Reattach to an open screen
$ screen -r [session_name]
copy

Detach from inside a screen
$ <Ctrl a><d>
copy

Kill the current screen session
$ <Ctrl a><k>
copy

Kill a detached screen
$ screen -X -S [session_name] quit
copy

SYNOPSIS

screen [-AmRvx -ls -wipe] [-d|-r] [-s shell] [-p number] [-S name] [cmd [args]]
screen -v
screen -h
screen -list
screen [-d|-r] [pid.tty.host]

PARAMETERS

-A
    Adapt all windows to the new display size. Useful when connecting from a smaller terminal.

-d
    Detach the specified screen session. If no session is specified, detaches and exits.

-m
    Forces screen to create a new screen session and doesn't try to attach.

-R
    Attempt to reattach a detached session. If a session isn't detached, attempt to create a new session. This is useful for always attaching to a screen session if one is available.

-r
    Reattach to a detached screen session. If more than one session exists, you must specify which session to reattach to.

-v
    Print the version number and exit.

-x
    Attach to a not detached screen. (Multi display mode). Enables multiple terminals to connect to the same screen session.

-ls
    List all active screen sessions.

-wipe
    Do some housekeeping. Delete dead pid files in the /tmp directory.

-s shell
    Specify the shell to use instead of the default.

-S name
    Name the screen session. This makes it easier to identify and reattach to specific sessions.

-p number
    Preselect the window number on attach.

DESCRIPTION

The screen command is a terminal multiplexer that allows you to start and manage multiple separate terminal sessions within a single terminal window or remote SSH session. It's invaluable for running long-running processes, detaching and reattaching to sessions, and managing multiple tasks without needing multiple terminal windows. Screen allows you to disconnect from a session and reconnect later, even from a different location, without interrupting the processes running within that session. This persistence makes it ideal for tasks like server administration, software compilation, or running interactive applications remotely. Screen provides features like window management (creating, switching, renaming), session sharing, and logging. It's a powerful tool for improving productivity and managing terminal-based workflows.

CAVEATS

Proper terminal handling within screen sessions can sometimes be tricky, especially with applications that heavily rely on terminal capabilities. Make sure the TERM environment variable is properly set inside the screen session. Nested screen sessions can also lead to unexpected behavior.

KEY BINDINGS

Screen uses a command character (Ctrl-a by default) followed by another key to execute commands within the session. Common commands include:
Ctrl-a c: Create a new window.
Ctrl-a n: Switch to the next window.
Ctrl-a p: Switch to the previous window.
Ctrl-a ": Display the window list.
Ctrl-a d: Detach the screen session.
Ctrl-a k: Kill the current window.
Ctrl-a ?: Display the key binding help.

CONFIGURATION

Screen can be configured using the ~/.screenrc file. This file allows you to customize key bindings, startup behavior, and other settings. Refer to the screen man page for detailed information on configuration options.

HISTORY

Screen has been a staple of Unix-like systems for many years, predating more modern alternatives like tmux. It was developed to address the need for persistent terminal sessions, allowing users to disconnect and reconnect without losing their work. Its widespread use in server administration and remote development has solidified its place as a crucial tool for many system administrators and developers.

SEE ALSO

tmux(1), nohup(1), at(1)

Copied to clipboard