Using GNU screen with examples

Have you even been in the situation where you were running a 3 hour copy or script on a remote machine, only to find that it broke at 2h 45min because your network connection or SSH connection dropped momentarily? If so, you know how painful that feels 🙂 Welcome to GNU screen, the utility which allows you to start a separate shell which will not be interrupted if your network connection breaks. Read on to discover how to use it and more!

In this tutorial you will learn:

  • How to install and use the GNU screen utility
  • How to configure the GNU screen utility to function better
  • Basic usage examples on how to use the GNU screen utility from the Bash command line

Using GNU screen with examples

Using GNU screen with examples

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other The screen utility can be installed using sudo apt-get install screen (or yum instead of apt-get for RedHat based systems)
Conventions # – requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires linux-commands to be executed as a regular non-privileged user

Example 1: Starting a screen session

$ sudo apt-get install screen
$ screen

A screen session starts, and enter may be required to close the splash screen. Next, we start a sleep process inside the screen session:

$ sleep 3600

Now you can press CTRL+a+d (press CTRL, press a, release a, press d, release d, release CTRL). You will see the following, in your original shell session (with a different process ID number):

[detached from 1130130.pts-10.abc]

Now that we are detached from the screen session, let’s scan for screen sessions live on the system:

$ screen -ls
There is a screen on:
    1130130.pts-10.abc  (06/08/20 13:41:43) (Detached)
1 Socket in /run/screen/S-abc.
$ screen -d -r 113  
$ sleep 3600  # Our sleep is still running

Next, you can use CTRL+C to terminate the sleep. You can then use exit to close our screen session and return is to our previous/original shell session. Note that you could have also used screen -d -r without the 112, as there was only one screen running.

In his example, we installed screen (may or may not be necessary depending on your Linux distribution used), opened a screen session and started as long sleep. Subsequently, we disconnected from the screen session (which you may think of as another shell which will continue to exist even if the user logs out or disconnects), and questioned the system on running screen session.

We then reconnected using the -d -r often-used shorthand. This shorthand basically means disconnected any pre-existing connection from the session being referred to, and connect me to it. It is handy to just learn and then always use this shorthand. The reason is that if you are remotely connected to a server, you will likely have multiple shell sessions open, and you may have already connected to a screen session from another shell session.

Our screen -ls session shows that our current screen session was Detached which should now make sense; we are not connected to/live within the screen session.

Example 2: Killing and wiping a screen session

Sometimes, especially when you start high profile/intensive jobs, your screen session may run into issues and terminate – not because of a bug in the screen utility – but because of your job running for example into OOM (out of memory) problems, etc. In this case you’ll see:

$ screen -ls
There is a screen on:
  1130130.pts-10.abc  (06/08/20 13:41:43) (Dead???)

As soon as a screen session is marked as Dead??? there is little that can be done with it. One way to cleanup this screen session is to kill the session, then wipe it:

$ kill -9 1130130
$ screen -wipe
There is a screen on:
  1130130.pts-10.abc  (06/08/20 13:41:43) (Removed)
1 socket wiped out.
No Sockets found in /run/screen/S-abc.

Example 3: Tuning screen to be more user friendly

One of the challenges with using screen is that – out of the box – it is not very user friendly. For example, it opens a splash screen every time you start it. Other items include at times potentially frustrating key bindings, and a limited scroll-back.

Most of these can be solved by creating a ~/.screenrc file. To do this, use your favorite text editor to create a .screenrc file inside your homedir. Note the leading dot makes the file invisible, and is significant for things to work correctly. Inside the file you can paste the following:

defscrollback 20000
startup_message off
autodetach on
vbell on
vbell_msg 'Bell!'
termcapinfo xterm* Z0=\E[?3h:Z1=\E[?3l:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l
termcapinfo xterm* OL=2000
bind .
bind h
bind ^h
bind x
bind ^x
bind ^\

The defscrollback 20000 sets the scroll-back to 20k lines. Next we turn off the splash screen with startup_message off. We also turn on auto-detachment and create a virtual bell (which generates no sound and uses visual output instead), and set it’s message (for on-screen display) to ‘Bell!’.

Next we set two termcapinfo’s for xterm where the first one avoids resizing the window, and the second one increases the output buffer for speed. Finally we remove some pre-existing keyboard shortcuts (by setting them to blank). This disables dumptermcap (bind .), disabled writing a copy of screen to disk (h and CTRL+h ^h), turn off screen locking (x and CTRL+x ^x) and finally killing all windows/exit screen (^\).

Final tip: if you want to access the scroll-back inside screen, you can often not just simply ‘scroll up’. Instead, press CTRL+a+esc (press CTRL, press a, release a, press ESC, release ESC, release CTRL), then use the cursor up key to scroll up as far as you like (up to the 20k lines you defined in ~/.screenrc). Press ESC again to exit the scroll/copy mode.

Conclusion

In this tutorial, we explored how to install and use the screen utility, and how to configure it so it functions better. We also looked some basic screen usage examples at the Bash command line. screen can be a versatile tool in the pocket of any Bash developer or user, and we invite you to post your own screen usage examples or comments below!

Enjoy!



Comments and Discussions
Linux Forum