Screen

From Leo's Notes
Last edited on 18 August 2023, at 07:10.

GNU Screen is a terminal multiplexer which allows multiple terminals to run within a single session. Screen includes many useful features including serial terminal support, the ability to lock, detach, and reattach a running terminal session, and much more.

Nowadays, it seems like tmux is the more popular terminal multiplexer and is what I use most often now. Nonetheless, Screen is still extremely useful especially if I just need to run something in the background.

Quick usage guide[edit | edit source]

After you have Screen installed using your distro's package manager, you can quickly get started with screen by learning a few basic commands.

Starting a screen session[edit | edit source]

Start a new screen session by running screen. You should also pass in a custom name for the screen with the -S option which will help you identify and re-attach to the screen session later.

$ screen -S test

This will start a new shell instance inside screen. You can see your current screen session with the custom name by running:

$ screen -ls
There are screens on:
        30922.test      (Attached)

The state of the session is 'Attached' since we're in that screen session. Any jobs you run in this screen session can persist even if you log out if you detach from this screen session.

Detach from and re-attaching to an existing screen session[edit | edit source]

Detach from a screen session by hitting Ctrl-a d. This key combination is written as C-a d in the man pages. If you list the screens running again, you can see that the screen is detached.

$ screen -ls
There are screens on:
        30922.test      (Detached)

You may reconnect to detached screens by their partial name or PID number.

$ screen -r 3     # by the partial (but unique) PID number
$ screen -r 30922 # or by the full PID number
$ screen -r t     # by the partial (but unique) screen name
$ screen -r test  # or by the full screen name

Navigating through scrollback history in screen[edit | edit source]

Scrollback history in a screen terminal isn't as easy to work with as a typical terminal because there is no scrollbar that you can interact with. Instead, to navigate around through the scrollback history, you will have to first hit Ctrl-a [ and then navigate with vi-like keys as outlined in the following table.

Key Movement
h Move the cursor left by one character
j Move the cursor down by one line
k Move the cursor up by one line
l Move the cursor right by one character
0 Move to the beginning of the current line
$ Move to the end of the current line.
G Moves to the specified line (defaults to the end of the buffer).
C-u Scrolls a half page up.
C-b Scrolls a full page up.
C-d Scrolls a half page down.
C-f Scrolls the full page down.
/ Search forward
? Search backward

If you ever need help finding the key bindings for all the different commands in Screen, hit Ctrl-a ? which will bring up the key bindings screen.

Tips & Tricks[edit | edit source]

Rename Screen Session[edit | edit source]

In a screen terminal, enter Ctrl-a :sessionname followed by the new name.

Eg:

# screen -ls
There are screens on:
        8570.test       (Attached)
## Hit Ctrl-a :sessionname Hello
# screen -ls
There are screens on:
        8570.Hello      (Attached)

Lock a Session[edit | edit source]

You can use screen to lock the virtual terminal by using the Ctrl-x key.

Running a command detached[edit | edit source]

To run a command in a screen session in the background using the -dm flag to specify the command.

~ % screen -S test -dm sleep 60
~ % screen -ls
There are screens on:
        353.test        (Detached)

For more complex commands, you may want to wrap the complexity within sh -c. For instance, to use && or to do pipe redirections, enclose it within a subshell.

~ % screen -S test -dm sh -c "sleep 60 && echo hello"
~ % screen -ls
There are screens on:
        353.test        (Detached)

Running screen through su[edit | edit source]

If you are running screen not directly through the terminal (such as through su), you will get the following error message:

$ screen 
Cannot open your terminal '/dev/pts/0' - please check.

You can remedy this using script:

$ script /dev/null
$ screen  # Rerun script while in a typescript session

Serial Console[edit | edit source]

You can use screen as a serial console as well.

screen /dev/ttyS0 <baud>[,<options>]

Common baud rates are 9600 or 115200.

Configuration[edit | edit source]

Customizing Screen using screenrc[edit | edit source]

When screen is started, it will read any configuration defined in /etc/screenrc and ~/.screenrc. You can customize screen by modify the either of these configuration files. System defaults should go into the /etc/screenrc file while personalized settings should go into ~/.screenrc.

Here is a sample ~/.screenrc configuration with customized status line and few additional options:

# Turn off the welcome message
startup_message off

# Disable visual bell
vbell off

# Set scrollback buffer to 10000
defscrollback 10000

termcapinfo xterm* OL=10000
# Change the xterm initialization string from is2=\E[!p\E[?3;4l\E[4l\E>
# (This fixes the "Aborted because of window size change" konsole symptoms found
#  in bug #134198)
termcapinfo xterm* 'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l'

# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

# Force Background Redraw
defbce on

Other options[edit | edit source]

Scrollback Buffer[edit | edit source]

Scrollback buffer can be changed by editing .screenrc. Define the following line with the number of lines to scroll back.

defscrollback 5000

Disable Visual Bell[edit | edit source]

To disable the visual bell for the current screen instance, hit:

Ctrl+a Ctrl+g

For a permanent fix, put the following into the ~/.screenrc file

vbell_msg "bell: window ~%" # Message for visual bell
vbellwait 2 # Seconds to pause the screen for visual bell
vbell off # Turns visual bell off

Force Background Redraw[edit | edit source]

Screen will sometimes mess with any background colors set by programs such as vim. This will cause weird visuals in the terminal where background colors are only rendered where there is text.

You can force screen to redraw the entire area such that the background colors are preserved by putting the following in your .screenrc file:

defbce on

Troubleshooting[edit | edit source]

Screen terminates when SSH disconnects[edit | edit source]

Whenever SSH disconnects, the server will send a HANGUP signal to Screen. By default, Screen will handle this signal by detaching the session and preserving your running programs in the background. However, if you configured screen with the autodetach off option, screen will handle this signal by terminating the session.

If this is not the desired behavior and you do want to have screen keep your sessions when you disconnect, ensure you have autodetach on set.