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.
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.
sudo apt update sudo apt install screen
To use screen just type “screen” in a shell/terminal and hit “enter”.
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”.
screen 4.03.01 (GNU) 28-Jun-15
Open a new window within the screen by typing “Ctrl+a” followed by “c”.
You have opened a new window within the screen. Now run “top” in this window.
To go back to the previous window press “Ctrl+a” followed by “p”.
Similarly to go to the next window press “n” instead of “p”.
You can open a few more windows by pressing “Ctrl+a” followed by “c”.
Ctrl-a c Ctrl-a c
To keep track of all the windows type:
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:
Close the shell. Now open a new shell and connect to your server and type “screen -r”.
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.
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.
You can reattach the screen by the following command.
To detach a screen any time and get back to the terminal which started the screen, type the following.
[detached from 1604.pts-0.ip-172-31-28-182]
The current screen will be detached and sent to the background. Now attach/resume the screen by typing “screen -r”.