What happens when you’re connected to a remote system, using a long-running program, and then the connection drops? The odds are, at a minimum, you’re going to have to restart the program, and in a worst-case scenario, you’ll have data corruption. To help get around this, some programs run in a window shell on the system. A very basic example of this is the screen
program:
[pgervase@pgervase ~]$ ssh root@rhel7dev.usersys.redhat.com
X11 forwarding request failed on channel 0
Last login: Wed Jan 27 12:10:06 2021 from xxxxxxxx.redhat.com
[root@rhel7dev ~]# screen
This opens my new shell on the rhel7dev
system. I’ll run the ping
command below from inside of that session:
[root@rhel7dev ~]# ping www.google.com
PING www.google.com (74.125.24.147) 56(84) bytes of data.
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=1 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=2 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=3 ttl=100 time=242 ms
I’ll now demonstrate how to detach from the session to simulate a network outage or to simply leave something running overnight. To do this, I hit Ctrl, hold that key down, then hit A, then hit D. That brings me back to the default SSH prompt and I am then able to run screen -ls
to see my detached session:
[root@rhel7dev ~]# screen -x
[detached from 25665.pts-0.rhel7dev]
[root@rhel7dev ~]# screen -ls
There is a screen on:
25665.pts-0.rhel7dev (Detached)
1 Socket in /var/run/screen/S-root.
[root@rhel7dev ~]#
[ You might also enjoy: Working with pipes on the Linux command line ]
To resume my screen session, I type screen -x
because there was only one session as an option. That brought me back to the screen session where the ping
command is still running:
[root@rhel7dev ~]# ping www.google.com
PING www.google.com (74.125.24.147) 56(84) bytes of data.
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=1 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=2 ttl=100 time=242 ms
<snipped>
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=19 ttl=100 time=242 ms
64 bytes from 74.125.24.147 (74.125.24.147): icmp_seq=20 ttl=100 time=242 ms
^C
--- www.google.com ping statistics ---
20 packets transmitted, 20 received, 0% packet loss, time 20278ms
rtt min/avg/max/mdev = 242.105/242.197/242.727/0.576 ms
[root@rhel7dev ~]#
I can have multiple screen sessions at once:
[root@rhel7dev ~]# screen -ls
There is a screen on:
25665.pts-0.rhel7dev (Detached)
1 Socket in /var/run/screen/S-root.
[root@rhel7dev ~]# screen
[detached from 25693.pts-0.rhel7dev]
[root@rhel7dev ~]# screen -ls
There are screens on:
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
2 Sockets in /var/run/screen/S-root.
[root@rhel7dev ~]# screen
[detached from 25706.pts-0.rhel7dev]
[root@rhel7dev ~]# screen -ls
There are screens on:
25706.pts-0.rhel7dev (Detached)
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
3 Sockets in /var/run/screen/S-root.
[root@rhel7dev ~]#
In each of those three screen sessions, I can have commands running or simply leave a session sitting at the prompt.
A default screen -x
will not work to resume a session now because of the multiple screens running:
[root@rhel7dev ~]# screen -x
There are several suitable screens on:
25706.pts-0.rhel7dev (Detached)
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
[root@rhel7dev ~]#
To attach to one of my sessions, I need to run screen -x
and add enough of the screen name to be unique:
[root@rhel7dev ~]# screen -x 257
[detached from 25706.pts-0.rhel7dev]
[root@rhel7dev ~]#
Rather than trying to limit yourself to just one session or remembering what is running on which screen, you can set a name for the session by using the -S
argument:
[root@rhel7dev ~]# screen -S "db upgrade"
[detached from 25778.db upgrade]
[root@rhel7dev ~]# screen -ls
There are screens on:
25778.db upgrade (Detached)
25706.pts-0.rhel7dev (Detached)
25693.pts-0.rhel7dev (Detached)
25665.pts-0.rhel7dev (Detached)
4 Sockets in /var/run/screen/S-root.
[root@rhel7dev ~]# screen -x "db upgrade"
[detached from 25778.db upgrade]
[root@rhel7dev ~]#
To exit a screen session, you can type exit
or hit Ctrl+A and then D.
Now that you know how to start, stop, and label screen
sessions let's get a little more in-depth. To split your screen session in half vertically hit Ctrl+A and then the | key (Shift+Backslash). At this point, you’ll have your screen session with the prompt on the left:
To switch to your screen on the right, hit Ctrl+A and then the Tab key. Your cursor is now in the right session, but there’s no prompt. To get a prompt hit Ctrl+A and then C. I can do this multiple times to get multiple vertical splits to the screen:
You can now toggle back and forth between the two screen panes by using Ctrl+A+Tab.
What happens when you cat
out a file that’s larger than your console can display and so some content scrolls past? To scroll back in the buffer, hit Ctrl+A and then Esc. You’ll now be able to use the cursor keys to move around the screen and go back in the buffer.
There are other options for screen
, so to see them, hit Ctrl, then A, then the question mark:
[ Free online course: Red Hat Enterprise Linux technical overview. ]
Further reading can be found in the man page for screen
. This article is a quick introduction to using the screen
command so that a disconnected remote session does not end up killing a process accidentally. Another program that is similar to screen
is tmux
and you can read about tmux
in this article.
À propos de l'auteur
I am a Senior Principal Security Architect at Verizon. Before that, I worked at Red Hat in various roles such as consulting and in the Solutions Architect where I specialized in Smart Management, Ansible, and OpenShift. In my free time, I enjoy spending time with my family, exercising, and woodworking.
Contenu similaire
Parcourir par canal
Automatisation
Les dernières nouveautés en matière d'automatisation informatique pour les technologies, les équipes et les environnements
Intelligence artificielle
Actualité sur les plateformes qui permettent aux clients d'exécuter des charges de travail d'IA sur tout type d'environnement
Cloud hybride ouvert
Découvrez comment créer un avenir flexible grâce au cloud hybride
Sécurité
Les dernières actualités sur la façon dont nous réduisons les risques dans tous les environnements et technologies
Edge computing
Actualité sur les plateformes qui simplifient les opérations en périphérie
Infrastructure
Les dernières nouveautés sur la plateforme Linux d'entreprise leader au monde
Applications
À l’intérieur de nos solutions aux défis d’application les plus difficiles
Programmes originaux
Histoires passionnantes de créateurs et de leaders de technologies d'entreprise
Produits
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Services cloud
- Voir tous les produits
Outils
- Formation et certification
- Mon compte
- Assistance client
- Ressources développeurs
- Rechercher un partenaire
- Red Hat Ecosystem Catalog
- Calculateur de valeur Red Hat
- Documentation
Essayer, acheter et vendre
Communication
- Contacter le service commercial
- Contactez notre service clientèle
- Contacter le service de formation
- Réseaux sociaux
À propos de Red Hat
Premier éditeur mondial de solutions Open Source pour les entreprises, nous fournissons des technologies Linux, cloud, de conteneurs et Kubernetes. Nous proposons des solutions stables qui aident les entreprises à jongler avec les divers environnements et plateformes, du cœur du datacenter à la périphérie du réseau.
Sélectionner une langue
Red Hat legal and privacy links
- À propos de Red Hat
- Carrières
- Événements
- Bureaux
- Contacter Red Hat
- Lire le blog Red Hat
- Diversité, équité et inclusion
- Cool Stuff Store
- Red Hat Summit