Using the Screen Command¶
Screen or GNU Screen is a terminal multiplexer. You can start a terminal session and then open multiple screens inside that session. Processes running in Screen will continue to run when their window is not visible, even if your session gets disconnected/times out - you can even close the terminal/connection to AWS instance and commands running on screens will continue!
The goal of this tutorial is to learn how to run and switch between multiple screen sessions. Check out the screen cheat sheet for commonly used commands.
Installing the Screen¶
To install screen, run the following command:
sudo apt update sudo apt-get install screen
ubuntu@ip-172-31-7-6:~$ sudo apt-get install screen Reading package lists... Done Building dependency tree Reading state information... Done screen is already the newest version (4.8.0-1). 0 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
The first line of code updates your instance to all latest software configurations. The second line of code does the actual installing.
To help tell the different terminal screens apart, type this command on the original terminal window (before you try out
echo "this is the original terminal window"
Do not clear the screen. We want to make sure the "this is the original terminal window" text lingers when we toggle back to this window later.
This message may not be visible in the AWS browser terminal.
Then, type the following into the same AWS instance to start a new screen session.
GNU Screen version 4.08.00 (GNU) 05-Feb-20 Copyright (c) 2018-2020 Alexander Naumov, Amadeusz Slawinski Copyright (c) 2015-2017 Juergen Weigert, Alexander Naumov, Amadeusz Slawinski Copyright (c) 2010-2014 Juergen Weigert, Sadrul Habib Chowdhury Copyright (c) 2008-2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury Copyright (c) 1993-2007 Juergen Weigert, Michael Schroeder Copyright (c) 1987 Oliver Laumann This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, see [Press Space for next page; Return to end.]
Press Space (twice) or Enter to get to the command prompt. You are now on a new screen!
If you plan to work with multiple screens, it helps to give each screen a unique name to tell them apart. You can name your screen by adding the
-S flag and a name:
screen -S <name>
Let's run a program in the new screen window to test it out:
top command is used to show the Linux processes. It provides a dynamic real-time view of the running system. Usually, this command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel.
While the top command is still running, create a screen tab by clicking Ctrl+A+C. You should see a new blank terminal.
Let's run a command in this blank terminal:
PING google.com (22.214.171.124) 56(84) bytes of data. 64 bytes from ord37s19-in-f14.1e100.net (126.96.36.199): icmp_seq=1 ttl=100 time=17.4 ms 64 bytes from ord37s19-in-f14.1e100.net (188.8.131.52): icmp_seq=2 ttl=100 time=17.4 ms 64 bytes from ord37s19-in-f14.1e100.net (184.108.40.206): icmp_seq=3 ttl=100 time=17.5 ms 64 bytes from ord37s19-in-f14.1e100.net (220.127.116.11): icmp_seq=4 ttl=100 time=17.5 ms .....
A ping test is a method of checking if the computer is connected to a network. It is used to ensure that a host computer which your computer tries to access is operating. It is run for troubleshooting to know connectivity as well as response time.
Now, we have three running "screens":
- Screen 1) The original ssh terminal you saw at login. You typed the
echocommand in it.
- Screen 2) A screen that's running the
- Screen 3) A screen that's running the
Think of the first screen command entered into your original terminal like opening a new browser window (i.e., our screen 2).
After starting a screen, you can open multiple tabs within the same window using Ctrl+A+C (i.e., our screen 3).
You can toggle between the multiple tabs using Ctrl+A+P. You can have commands running in each tab.
Detaching the screen allows you to go back to the original window/panel.
If you start another screen from the original terminal window, it would be like opening another browser window, instead of adding a tab to an existing window.
Switching between tabs¶
To switch between the two new tabs, i.e., our screen 2 and screen 3, type Ctrl+A+P. You cannot toggle to the original terminal screen (i.e. screen 1) with this shortcut.
To detach a screen session and return to your original SSH terminal, type Ctrl+A+D. You will be taken back to the terminal window with the
To list your current screen sessions type:
ubuntu@ip-172-31-7-6:~$ screen -ls There is a screen on: 2683.pts-0.ip-172-31-7-6 (02/09/21 19:41:19) (Detached) 1 Socket in /run/screen/S-ubuntu.
screen -ls on the original terminal shows you only one screen ID because you have one screen (i.e. browser window) with two tabs. You can open another screen window by typing
screen into the original terminal.
From the output of
screen -ls above, we use the screen id to reconnect to the screen:
screen -r 2683.pts-0.ip-172-31-7-6
screen -r 2683
You should see screen 2 that you previously created. Once again you can toggle between screen 2 and screen 3 tabs by typing Ctrl+A+P
You can quit the
top output by typing
q or Ctrl+C
To end a screen session, toggle into the session and type:
If no other screen sessions are open, you will fall back to the original SSH terminal (screen 1). If another screen session is open, you will enter that screen session. You can type
exit until all screen sessions are closed.
exitinto a screen permanently closes that screen session and all the analyses that are conducted in it.
You may need to type
exittwice to get the "screen is terminating" message. But be sure to check!
exit There are stopped jobs. exit [screen is terminating]
exittoo many times will exit the entire terminal!
Don't forget to check out our screen cheat sheet if you need a quick reference to screen commands.