How to use Screen in Linux?

Screen is a Linux utility that allows you to run a command in a terminal window that will continue to run even if you close the terminal window or log out of the system. This is for users who want to work with a command that takes hours to complete, and the user needs to use the terminal while it is being executed. It can also be used to run commands in the background. Now the problem comes with how to use it in Linux.

This article clarifies everything about using Screen in Linux, where users will understand the following after going through it.

Let’s get started!

How to Install Screen on Linux?

The screen is a command line utility that is pre-installed in most Linux distros, and you can check the current version of it using this command.

$ screen --version

As seen above, Screen is installed, and the version is 4.09.00. If you see a message saying, “Screen not found but can be installed with a command”, you must install it using either of the following commands based on your distro.

$ sudo apt install screen         # Debian, Ubuntu, and Linux Mint]
$ sudo yum install screen         # RHEL, CentOS and Fedora
$ sudo pacman -S screen           #Arch Linux
$ sudo zypper install screen      #OpenSUSE   

Now that you’ve confirmed the installation of Screen on your system. Let’s launch it using this command which will result in a welcome screen, as seen below.

$ screen

Let’s explore some usages of the screen in Linux.

How to Detach Terminal Session Using Screen?

While working, there may be an event where you execute a command that takes a long time to complete its process, and you need to take back control of your currently opened terminal. For example, you’re using the ping command for google.com, as seen below.

$ ping google.com

The above-executed command will take considerable time, and you’d feel left behind even if it is a matter of minutes. Let’s do some magic using the Screen utility.

How to Detach Terminal Session Using Screen?

Using the Screen utility, users can easily detach the process from the terminal, and that process will be executed even if you close the terminal. To do that, you need to run the Screen Command before the command(s) you want to execute. To run the Screen Command, use this command.

$ screen

You won’t see any confirmation or anything like that, so let’s detach the process of using the ping command for google.com.

$ ping google.com

When the command is executed, we will use “Ctrl + a + d” to detach the process keeping in mind that we’ve run the Screen command before doing. It’ll show the following output.

The above image shows that the process is detached from the terminal and will be executed in the background giving back the user control of the terminal. In the next section, we will re-attach a process that was detached.

How to Re-attach the Terminal Session Using Screen?

If you want to see the progress of the process/command detached from the terminal, in simple words, re-attaching. The following syntax of the Screen command is used to re-attach the process in the terminal.

$ screen -r processID-OR-PID

In the above command, ‘-r’ is a flag used to re-attach the process while the processID or PID is the running ID.

To find out the process’s PID, use this command.

$ screen -ls

In the above image, three processes are detached, and the PID of each of them starts with a numeric value which is to be noted to re-attach using this command.

$ screen -r 3614

In this example, we’re re-attaching the process with PID ‘3614’, and once we execute the above command, we’d see the process back in the terminal.

The above image represents the ping command (ping google.com) that was used earlier.

How to Lock the Terminal Using Screen on Linux?

Using the Screen utility, you can use your user’s password to re-attach the process(es). This is a useful command to hide what you’re doing on the system because nobody can view the processes without a correct password, which is your user password. To do that, you need to press ‘Ctrl + a + x” which will display the following screen.

This is yet another great use of Screen Utility because using it, you can hide all the processes, whether attached or detached from the terminal.

Conclusion

Using the Screen Command line utility, users can maximize their control over the terminal by attaching and detaching the processes that take too long to execute. This post has briefly explained the usage of screen utility in Linux.