"Screen "* is a very handy program which has many functions. It is very convenient to use this utility when connecting to the system remotely via ssh. But its most common use is to minimize programs that can not do this themselves, with the ability to return to them later. This can be useful when working with programs that are not implemented as a "daemon + client", but as a whole, and do not work without running the interface.

Installation screen

To install on Ubuntu, run the command

sudo apt-get install screen  

After that, you need to run screen:

screen  

After starting you will see either a command prompt or a "welcome screen", pressing SPACE or ENTER on which you will also get into the shell. In this case, all commands will be run already "inside" the screen.

In order to quit the screen (quit, not disconnect), just quit all the shells open in it, or press Ctl-a , and answer "y" to the question "do you really want to quit".

To disconnect from the current screen without closing or terminating the session, you can use the combination Ctrl-a d.

Connect to screen

If you already have a running session and want to connect to it, then

  • if there is only one session, a simple command will suffice:
screen -x  
  • if there are several sessions, then: you can see the list of running screens with the command screen -ls:
screen -ls  
There are screens on:  
        2672.pts-0.ubuntu (Detached)
        2437.pts-0.ubuntu (Detached)
2 Sockets in /var/run/screen/S-diesel.  

Choose the screen we want, and join it:

 screen -x 2672.pts-0.ubuntu 

If you want to change the name of the session and want to run and use several screen sessions for different tasks. Come up with a name, then start a new screen like this

  $ screen -S "job1"

where job1 is the name of our "rename" session. Now we will see a much clearer one in -ls:

$ screen -ls
There are screens on:  
        2672.pts-0.ubuntu (Detached)
        2795.job1 (Detached)
        2437.pts-0.ubuntu (Detached)
3 Sockets in /var/run/screen/S-diesel.  

and we can connect to the job1 session, just by specifying its name:

$ screen -x job1

Switching between windows screen

All sessions are saved even when you are disconnected from the server and whatever is running in them continues to run

``Command execution monitoring

If you have several windows open, it may be useful to be able to monitor the activity in any of them. Go to the window with this command, press Ctrl - a m, you will see an inscription like this: "Window 0 (bash) is now being monitored for all activity". When something in the window will change, screen will beep, and in the list of windows the "@" symbol will appear after the number.

Screen scrolling in screen

To view the output of a command that has gone above the window boundary, you need to press Ctrl-a [

This will toggle the screen into Copy mode. To exit this mode just press Esc.

Configure screen

The screen is configured with the ~/.screenrc file, in your home directory. For example you could write there:

caption always "%{= 45}%{+b w}Screen: %n | %h %=%t %c"  
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"  

This will give you an all time visible list of open windows, and other useful information at the bottom of the screen.

Commands from the /etc/screenrc file and the .screenrc file in the user's home directory are also run by default when you start Screen. Many commands that can be used in configuration files have been covered in the tables above under "Text command". Below is an example of a Screen configuration from a .screenrc file:

# Disable the display of license information when starting Screen
startup_message off  

# Open a shell to work
chdir  
screen -t Work  

# Open a shell to control the configuration
chdir /etc  
screen -t Configuration  

# Open shell to view logs
chdir /var/log  
screen -t Logs  
# Select first window after startup
select 0  

This configuration file opens up three windows named Work, Configuration, and Logs in the user's private directory, in/etc, and in /var/log, respectively. After starting, the first window named Work will be displayed

Finish Screen

If for some reason the screen session stops responding, you can terminate it. To do this, connect to the desired session, then press Ctrl - a and type the command ":quit ".

Updated Aug. 17, 2018