6.3.9 Running applications in the background using Screen

Banner When working in a terminal, there is often a need to run long-running tasks that you do not want to interrupt when the connection is lost or the terminal is closed. It can also be useful to work with multiple processes or sessions simultaneously in the same terminal window. To resolve this issue, you can use the Screen utility.

Screen is a console utility that allows you to open an unlimited number of independent virtual terminals (windows) in an active SSH session. It provides the ability to run applications in the background, switch between different sessions, disconnect from sessions and reconnect to them later, even after the connection is lost. Screen is widely used for remote management of servers via SSH, as it allows you to keep running processes when the connection is lost.

Installation, launch and display of help Screen

In a number of Linux distributions, Screen is present by default. But it may happen that the utility is not installed. You can complete the installation using the following commands.

Important: installation must be performed with administrator (root) access level. You can go to the administrator level using su or sudo.

Command for Ubuntu/Debian:

  1. apt-get install screen

For CentOS/RHEL:

  1. yum install screen

Immediately after installation, Screen is ready to use. To get started, let’s launch Screen without parameters, creating a new session:

  1. screen

Immediately after launch, a window will appear with information about the creators and the license, and will wait for you to press the spacebar or Enter.

Please note: Screen commands are executed using the hotkeys Ctrl + a. You need to press and release Ctrl + a, and then enter the required command. For example, to list all commands, first press and release Ctrl + a, and then type ?

  • Ctrl + a -> ?

If it doesn’t work, try again, following the sequence of clicks:

  1. Press Ctrl + a
  2. Release Ctrl + a
  3. Enter ?

Other commands in Screen are executed in the same way. First press and release Ctrl + a and then enter the desired command, for example:

  • Ctrl + a -> c – create a new screen
  • Ctrl + a -> n – switch to next screen
  • Ctrl + a -> : – enter a command to send to the background process

Note the use of -> to indicate the sequence, press Ctrl + aenter command.

Examples of practical application

To begin with, in the current screen (it is zero in the list), let’s run:

  1. ping 8.8.8.8

Now let’s introduce

  • Ctrl + a -> c

With this team we created a new screen, and now they work simultaneously.

Let’s run it:

  1. ping 8.8.4.4

To switch between screens, you can either specify the screen number or select a screen from the list:

  • Ctrl + a -> 0 – will take us to screen 0.
  • Ctrl + a -> " – will display a menu for selecting a screenshot.
  • Ctrl + a -> n – switches to the next screen.
  • Ctrl + a -> p – switches to the previous screen.

Now let’s minimize Screen with our pings. To do this we will use a hotkey

  • Ctrl + a -> d

After executing the command, Screen will be minimized and we will be taken to our terminal. In order to return, run the utility with the -r switch:

  1. screen -r

Sometimes trouble happens with lost screenshots; in order to connect to them, you need to run the command:

  1. screen -x

then find the number of the lost screen in the output and return it with the command:

  1. screen -x n

where n is the same number.

You can return to the session even after the SSH connection is closed when using Screen remotely, which makes it indispensable when performing many jobs.

You can quickly create a new screen with a running program, for example, wget of a large file: Using the -S switch when running Screen, you can specify the name of the screen:

  1. screen -S download wget http://mirror.thehost.ua/debian-cd/7.8.0/amd64/iso-dvd/debian-update-7.8.0-amd64-DVD-1.iso

In our example, the window will be named download. There is always the option to rename the screen with a hotkey:

  • Ctrl + a -> A

You can view all active screens with the command:

  1. screen -list

If you need to connect to a screen running in another terminal, you can do this using the command:

  1. screen -x download

Screenshots can be hidden from strangers using a hotkey:

  • Ctrl + a -> x

Managing windows

Screen has the ability to divide the main window into several areas (panels) for more efficient organization of your workspace. You can create both horizontal and vertical sections and easily switch between them.

Horizontal separation

To split a window horizontally, use a hotkey:

  • Ctrl + a -> S
Horizontal
An example of horizontal separation.

Vertical separation

To vertically split a window, use the following combination:

  • Ctrl + a -> |

After dividing the window into panels, you can switch between them using the command:

  • Ctrl + a -> Tab

If after switching to a new panel nothing can be done, then the screen has not yet been launched on this panel. You can either create a new one or choose from existing ones.

You can close a single panel or all panels except the current one like this:

  • Ctrl + a -> X – close the current panel.
  • Ctrl + a -> Q – close all panels except the current one.

The ability to divide the main window into horizontal and vertical areas allows you to effectively use the workspace to simultaneously work with different tasks and processes.

Vertical
An example of a vertical separation.

Write output to file

Everything that is output in the Screen window can be written to a file using the command:

  • Ctrl + a -> H

After entering this command, Screen will ask for the file name to record.

Blocking access to Screen

To block Screen from unauthorized access, you can use the command:

  • Ctrl + a -> x

After entering this command, you will be asked for a password. To unlock you will need to enter the same password.

Please note: when entering commands in Screen, both lowercase and uppercase characters are used. To switch case, use the Shift or CapsLock keys on your keyboard.