Have you ever wondered how you can run a long process in a remote Linux machine over ssh without worrying about connection getting dropped. Usually when you are running a long process and if connection with remote machine drops due to any reason, your process gets terminated. In order to overcome this there is a utility called screen in Linux. Most Linux distros come pre-installed with screen utility. If not we can install them with no effort. Screen command provides us with the ability to create and use multiple shell sessions allowing us to detach and reattach to the screen any time if you haven’t terminated the session. When you reattaches to a session, the session will have the the process running/completed and will be just like where you left.

Install Screen Utility

Most Linux distributions will have screen installed. To verify you can use the command:

$ screen --version
Screen version 4.08.00 (GNU) 05-Feb-20

If you don’t have screen installed, you can install by the following command

For Ubuntu and Debian

$ sudo apt update
$ sudo apt install screen

For Fedora and CentOS

$ sudo yum install screen

How to start Linux Screen?

To start a linux screen use the following command

$ screen

To create a named session for you to identify the task/screen use the command below

$ screen -S screen_name

How to detach from a screen

Use this command to detach from screen any time

Ctrl+a d

Reattach to a screen

If you have only one screen created you can use the following command to attach again

$ screen -r

If you have multiple screens running in the machine, you can use the screen id to attach to the session. To find the screens in the machine, you can use the following command:

$ screen -ls
There are screens on: 
    30495.my_screen_1 (Detached)
    30497.task_two_update (Detached)
2 Sockets in var/run/screen/S-webuser.

To attach to one of the screen you can use the id like shown below

$ screen -r 30495

Now you don’t have to worry about getting your connection disconnected/ causing a broken pipe and getting your process terminated when running long tasks. Start using screen for tasks and you will never regret the decision. A very handy utility when working remotely on Linux servers.