Cookie

The command “screen” to emulate multiple shell sessions

Published by TheJoe on

Estimated reading time: 4 minutes

Today we see how to use screen.

I recently had to run commands that expected a very long duration on a remote server. In my latest article, eg, I described how to transfer data between an older NAS and a modern NAS via FTP. In that case I used wget with the topic -b putting the process in the background so i could disconnect the ssh session and the process continued to the end.

To put even a very long process in the background, simply add a & at the end of the command. Unfortunately I'm very careless and often expect the command to return output in a short time, while in reality I realize I would have saved my time by putting the cursed character at the bottom of the command. An example?

~# updatedb &

Instead, we want to talk about the times we've run the same type of command, but then the connection fell apart for the most disparate causes? Who hasn't?

Luckily it exists screen which does this and much more… and now we will see.

Screen is able to manage infinite virtual terminal windows in a single session. This means that having to run several commands on a remote machine we won't have to open an SSH connection for each command we run. In one session we have infinite possibilities “windows” virtual and endless sessions.

Screen processes remain active even when we close the main session, unless we voluntarily terminate them. This means that in case of accidental disconnections the process will go on until the end and we will be able to reconnect to the abandoned session painlessly.

The installation

Screen comes pre-installed on all major distributions. Check that you have it in your packages.

~# screen --version
Screen version 4.08.00 (GNU) 05-Feb-20

For architectures based on apt:

~# apt install screen

For architectures based on rpm:

~# yum install screen

Start screen

To start a screen session just type screen in a console.

~$ screen

This command will create a screen session, will create a new virtual window within the session and start a shell in that window.

Now that we've started the session we can find a list of commands by typing: Ctrl + a ?.

Ed ecco l’output:

                           Command key:  ^A   Literal ^A:  a

    break       ^B b           license     ,              removebuf   =         
    clear       C              lockscreen  ^X x           reset       Z         
    colon       :              log         H              screen      ^C c      
    copy        ^[ [           login       L              select      '         
    detach      ^D d           meta        a              silence     _         
    digraph     ^V             monitor     M              split       S         
    displays    *              next        ^@ ^N sp n     suspend     ^Z z      
    dumptermcap .              number      N              time        ^T t      
    fit         F              only        Q              title       A         
    flow        ^F f           other       ^A             vbell       ^G        
    focus       ^I             pow_break   B              version     v         
    hardcopy    h              pow_detach  D              width       W         
    help        ?              prev        ^H ^P p ^?     windows     ^W w      
    history     { }            quit        \              wrap        ^R r      
    info        i              readbuf     <              writebuf    >         
    kill        K k            redisplay   ^L l           xoff        ^S s      
    lastmsg     ^M m           remove      X              xon         ^Q q      

^]   paste .
"    windowlist -b
-    select -
0    select 0
1    select 1
2    select 2
3    select 3
4    select 4
5    select 5
6    select 6
7    select 7
8    select 8
9    select 9
I    login on
O    login off
]    paste .
|    split -v
:kB: focus prev

Give a session a name

A renamed session is convenient when we need to recall it. For example by renaming a session “wget_debian” we will know at a glance by looking at the list of active sessions that in that session we launched the Debian ISO download.

~$ screen -S wget_debian

Familiarize yourself with the screen windows

When you start a session screen creates a virtual window with a shell inside.

Look here:  LibreOffice "Calc": define the first row of each page

Screen windows are not to be confused with sessions. A window (any windows you want) is contained in a session, from which you can disconnect without it ending the processes contained.

You can have several windows in one screen session.

To create a new virtual window type Ctrl+a c and the first number from 0 a 9 will be assigned.

Here are some of the most common commands for managing screen windows:

  • Ctrl + a c Create a new window
  • Ctrl + a " Show a list of windows
  • Ctrl + a 0 (zero) Switch to the window “0” (substitute “0” with the number relating to the desired window
  • Ctrl + a A Rename the current window
  • Ctrl + a S Splits the portion of the screen horizontally
  • Ctrl + a | Splits the portion of the screen vertically
  • Ctrl + a tab Move focus to next region
  • Ctrl + a Ctrl + a Switch from the previous window to the next one
  • Ctrl + a Q Close all regions
  • Ctrl + a X Close the current region

Unplug (detach) your terminal from the current session

One plus, as already said, is to be able to continue working by letting the screen sessions work in the background, while the user can work on something else.

Ctrl + a d

Reconnect (resume) your terminal to a session

To restore connection with an abandoned session:

~$ screen -r

If there are multiple sessions on a single machine, you will also need to add the session ID after the argument -r.

To find the ID of the session to reconnect to, use the following command:

~$ screen -ls
There are screens on:
13871.pts-3.joe-toshiba (29/11/2022 11:59:39) (Detached)
13836.pts-3.joe-toshiba (29/11/2022 11:59:20) (Detached)
13811.pts-3.joe-toshiba (29/11/2022 11:58:49) (Detached)
3 Sockets in /run/screen/S-joe.

If we want to go back to the session 13836.pts-3 we type the command:

~$ screen -r 13836

Conclusions

Today we learned how to use screen to create multiple windows from a single session, navigate between the windows and to disconnect and reconnect with the various sessions… but there is much more to learn su screen.


TheJoe

I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.