Skip to content

Screen

Created: 2017-02-04 11:15:39 -0800 Modified: 2021-10-02 11:08:32 -0700

  • List all available screens: screen -ls

  • Attach to a screen shown in “screen -ls”: screen -rd <name of screen>

    • Note: I believe “attached” just means that that’s where your commands will go (switching between windows in the screen, killing screens, etc.).
  • Kill all screens: killall screen

  • Kill a single screen:

    • ctrl+a, k
    • ctrl+D (this is just a logout)
    • ”$ exit” (this is just a logout)
  • To rename a screen: ctrl+a, capital A

  • Ctrl+A and then a shortcut is the way to interact with screen easily

    • I can do this flow:
      • <first time only> screen
      • Ctrl+A c - make a window
      • Ctrl+A A - title it “client”
      • <launch the client>
      • Ctrl+A c - make another window
      • Ctrl+A A - title it “server”
      • <launch the server>
      • Close Putty if I want
      • …later
      • Reconnect with Putty
      • screen -rd - reattach to the screen I had earlier
      • Ctrl+A ” - choose which window I want
      • Ctrl+D, d: detach back to the shell
  • Scrolling a screen: ctrl+a, ctrl+[

    • Then use arrows or page [up|down]
  • screen -rd: this reattaches to an existing screen, detaching first if necessary (e.g. if Putty was timed out and your bash session was still active).

    • screen -list will show any active screen.
  • To create a new screen window from inside screen, just do “screen -t name”. You may need the “-p” option.

  • To make it so that your Bash profile is automatically loaded when you launch “screen”, you’ll need to edit your ~/.screenrc file

startup_message off

I think this may be all that’s necessary for using bash_profile actually…

Section titled I think this may be all that’s necessary for using bash_profile actually…

shell -$SHELL

”-t” is title. The number is the number that shows up when you press ctrl+A “

Section titled ”-t” is title. The number is the number that shows up when you press ctrl+A “

The “-l” at the end of “bash” is to get it to simulate a login, that way

Section titled The “-l” at the end of “bash” is to get it to simulate a login, that way

you’ll have access to the bash_profile functions.

Section titled you’ll have access to the bash_profile functions.

screen -t bash 0 bash -l

screen -t bldeploy 1 bash -l

select 0

Annotated Bash script for starting up commands in screen (written by HiDeoo):

WARNING #1: I’ve found that I need to add “sleep 0.1” after creating the screen or else subsequent commands don’t always work.

WARNING #2: your .screenrc file can get in the way of this sometimes. For example, if my screenrc is set to create tabs like “bash” and “htop”, then I don’t think “thingy1” below ever gets created. According to HiDeoo:

thingy1 was the first window created automatically by screen at launch which we renamed using the CLI options, but this first window, which has an id #0 (starts at 0) was also renamed in the .screenrc with screen -t bash 0 bash -l (note the id #0) which lead to the conflict and the .screenrc won in this case ^^ No idea for the sleep tho, seen this several time, never had to use it but I'm just a dev using screen, not a sysadmin or something who could give more infos / details.

Also, if you find .screenrc files more readable / usable than a script, you can specify a path for a screenrc to use to screen with the -c option like screen -c useThisScreenrcInsteadOfTheDefaultOne.

#!/bin/bash
# This script will create a new screen session named "deploy".
# To connect to the session after running the script, use 'screen -r deploy'.
# General knowledge:
# -X option will send the following command to a screen session.
# First, let's destroy the "deploy" session if it already exists using the 'quit' command.
# If the session do not exists, the message "No screen session found." will be printed.
screen -S deploy -X quit
# Let's create a new session named "deploy".
# Screen automatically create a window, we rename it to "thingy1" using the -t option.
screen -dmS deploy -t thingy1
# I, Adam, need this sleep here or else it sometimes won't find the new screen.
sleep 0.1
# We now send a command to the "deploy" session to create a new window. We'll use the screen command inside a screen
# session to do that.
# The new tab will be name "thingy2".
screen -S deploy -X screen -t "thingy2"
# We now want to run things inside our 2 new windows.
# We specify the window we want to run the command to using the -p option.
# We'll use the screen 'stuff' command to insert a string in the input buffer and r to "Enter" / run the command.
screen -S deploy -p thingy1 -X stuff $'echo Welcome to tab number 1r'
screen -S deploy -p thingy2 -X stuff $'echo Welcome to tab number 2r'

What’s nice about this screenrc is that you’ll be able to see the windows as “tabs” at the bottom of the terminal so that you can easily switch with ctrl+A, <number of tab>.

shell -$SHELL

GNU Screen - main configuration file

Section titled GNU Screen - main configuration file

All other .screenrc files will source this file to inherit settings.

Section titled All other .screenrc files will source this file to inherit settings.

Allow bold colors - necessary for some reason

Section titled Allow bold colors - necessary for some reason

attrcolor b “.I”

Tell screen how to set colors. AB = background, AF=foreground

Section titled Tell screen how to set colors. AB = background, AF=foreground

termcapinfo xterm ‘Co#256:AB=E[48;5;%dm:AF=E[38;5;%dm’

Enables use of shift-PgUp and shift-PgDn

Section titled Enables use of shift-PgUp and shift-PgDn

termcapinfo xterm|xterms|xs|rxvt ti@:te@

Erase background with current bg color

Section titled Erase background with current bg color

defbce “on”

term xterm-256color

defscrollback 30000

backtick 101 30 15 $HOME/bin/mailstatus.sh

Section titled backtick 101 30 15 $HOME/bin/mailstatus.sh

hardstatus alwayslastline

Very nice tabbed colored hardstatus line

Section titled Very nice tabbed colored hardstatus line

hardstatus string ’%{= Kd} %{= Kd}%-w%{= Kr}[%{= KW}%n %t%{= Kr}]%{= Kd}%+w %-= %{KG} %H%{KW}|%{KY}%101`%{KW}|%D %M %d %Y%{= Kc} %C%A%{-}‘

“Ctrl+A A” becomes your shortcut instead of just “Ctrl+A”