The screen or GNU screen is a terminal multiplexer. screen command in Linux provides the ability to launch and use multiple shell sessions from a single SSH session. When a process is started with screen, the process can be detached from session and then can reattach the session at a later time. When the session is detached, the process that was originally started from the screen is still running and managed by the screen itself. The process can then re-attach the session at a later time, and the terminals are still there, the way it was left.
Using screen you can run any number of console-based-applications, interactive command shells, course-based applications, etc.
The screen command allows us to:
- Use multiple shell windows from just one SSH session
- Keep a shell session active even when there are network disturbances
- Manage connections to shell from multiple locations
- Keep a long running session alive even after exiting a sell session
screen command lets you reattach to a previous session so that you can finish your task. This article focuses on this specific use of screen command.
How to install screen command
It is quite simple to install screen.
– On Debian, run the following command:
apt-get install screen
– For Ubuntu:
sudo apt-get install screen
– For Fedora and CentOS:
yum install screen
How to use screen command
screen allows you to create one or more sessions in your current SSH terminal.
Run the following command to start it:
The above command will create a screen session or window (although you don’t see it as such) in your current SSH terminal.
Press Space or Return to get to the command prompt.
You’re left at the command prompt, and nothing much seems to have happened. However, you’re now running a shell inside a multiplexed terminal emulator.
Here are the most important screen commands that you need to control screen. All these start with CTRL a in order to to distinguish them from normal shell commands:
Ctrl a c – Creates a new screen session so that you can use more than one screen session at once
Ctrl a n – Switches to the next screen session
Ctrl a p – Switches to the previous screen session
Ctrl a d – Detaches a screen session (without killing the processes)
Ctrl a w – It display the list of all the windows currently opened.
Ctrl a A – It rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a + w.
Ctrl a Ctrl a – It back to the last windows used
Ctrl a k – It close the current windows (kill)
Ctrl a S – It split the current windows horizontally. To switch between the windows, do Ctrl-a + Tab
Ctrl a |- It split the current windows vertically
Ctrl a r – It reattach a detached screen session
Ctrl a [ – It start the copy mode
Ctrl a ] – It paste the copied text
In order to close a screen session where all tasks are finished you can type:
On your normal SSH terminal, you can run:
To get a list of your current screen sessions you can run:
There are screens on: 2477.pts-0.server1 (Detached) 2522.pts-0.server1 (Detached) 2 Sockets in /var/run/screen/S-root.
To reconnect to one of these sessions, run:
screen -r 2477.pts-0.server1
where 2477.pts-0.server1 is the name of one of the sessions from the screen -ls output.
To leave and finish a screen session, finish all current tasks in it (top can be finished by typing q, tail -f /var/log/mail.log can be finished by typing CTRL c) and then type:
You will then fall back to another screen session (if you use more than one) or to the normal SSH terminal, if no more screen sessions are open.
If you want to learn more about screen, run:
Keep processes running despite a dropped connection. What can you do?
The screen command is most used for SSH session because it helps to continue your work after a disconnection without losing the current processes in progress. Once your connection is back up, log in to your system with SSH again and run:
From the results pick one session (e.g. 2477.pts-0.server1) and reattach to it:
screen -r 2477.pts-0.server1
If you picked the right session, you should find your kernel still compiling (if it hasn’t finished in the meantime) so that you can continue your work.