How to Use Screen on Ubuntu 16.04

GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

A remote connection can be interrupted due to many reasons including network outrage. In such a scenario, there is a high probability of losing SSH access in the middle of a task. This may terminate the session and the ongoing job while the system in an unpredictable state. The GNU screen can come to rescue in such a situation.

Installation

It comes pre-installed on Ubuntu Server 16.04. If for any reason, it has been removed, you can install it by the following command.

[code language=”bash”]
sudo apt update
sudo apt install screen
[/code]

Usage

To use screen just type “screen” in a shell/terminal and hit “enter”.

[code language=”bash”] screen [/code]

It will display a long message. Just hit “enter”. You are in a screen terminal now. To verify type “Ctrl+a” followed by “v”. You should see an output similar like “screen 4.03.01 (GNU) 28-Jun-15″.

[code language=”bash”] Ctrl-a v [/code]

[code language=”bash”] screen 4.03.01 (GNU) 28-Jun-15 [/code]

Open a new window within the screen by typing “Ctrl+a” followed by “c”.

[code language=”bash”] Ctrl-a c [/code]

You have opened a new window within the screen. Now run “top” in this window.

[code language=”bash”] top [/code]

To go back to the previous window press “Ctrl+a” followed by “p”.

[code language=”bash”] Ctrl-a p [/code]

Similarly to go to the next window press “n” instead of “p”.

[code language=”bash”] Ctrl-a n [/code]

You can open a few more windows by pressing “Ctrl+a” followed by “c”.

[code language=”bash”]
Ctrl-a c
Ctrl-a c
[/code]

To keep track of all the windows type:

[code language=”bash”] Ctrl-a w [/code]

This will output a numbered list of windows starting from 0 with the current window marked by a star. You can switch to a particular window by pressing “Ctrl+a” followed by the screen number. To switch to the first window type:

[code language=”bash”] Ctrl-a 0 [/code]

Close the shell. Now open a new shell and connect to your server and type “screen -r”.

[code language=”bash”] screen -r [/code]

You should be taken back to where you left off.

If, instead, you see a message similar to the following then the screen has not detached.

[code language=”bash”]
There is a screen on:
1710.pts-0.ip-172-31-28-182 (02/04/18 16:59:55) (Attached)
There is no screen to be resumed.
[/code]

You can reattach the screen by the following command.

[code language=”bash”] screen -x [/code]

To detach a screen any time and get back to the terminal which started the screen, type the following.

[code language=”bash”] Ctrl-a d [/code]

[code language=”bash”] [detached from 1604.pts-0.ip-172-31-28-182] [/code]

The current screen will be detached and sent to the background. Now attach/resume the screen by typing “screen -r”.

[code language=”bash”] screen -r [/code]

 

Leave a Reply

Your email address will not be published. Required fields are marked *