The screen command is very helpful when you need to run long-time commands (such as upgrades) or you need to "share" the SSH session with another remote user. A dedicated Shell is running in the screen process, which can be detached and re-attached again.
Let's create a new detached screen session named "upgrade":
ck@linux:~$ screen -dmS upgrade
We can find the detached screen session using screen -list:
ck@linux:~$ screen -list
There is a screen on:
34002.upgrade (07/06/2022 02:20:27 PM) (Detached)
1 Socket in /run/screen/S-ck.
Re-attach the screen:
ck@linux:~$ screen -r upgrade
And we're now in a separate Shell inside the screen session:
Note: To detach from within a screen session, use the following keyboard combination: [CTRL]+[a] followed by [d].
We can now launch some commands which could take some time, such as upgrading the local packages. Before we do that, let's get some information about the current distribution.
Now let's run the actual apt commands to update the local packages. We can now see that part of the output above (containing the information about the OS release) disappears:
At this point we have reached the relevant part of this blog article. When (re-)attaching a running screen session (using screen -r sessionname), only the last lines of output is shown. That number of lines actually depends on the current window size: A small terminal window only shows so much of the screen session output, a larger window shows more historical output.
To make this worse, there is no scrollbar available. Doesn't matter if you've been in the screen session since its beginning or (re-)attached to the session at a later point in time. When trying to scroll using using a wheel mouse, this is interpreted as character input and results in weird characters (^[[A) showing up in the screen session:
This is especially frustrating if you need to check the historical output from a screen session - usually the reason for running a process inside a screen session.
But screen wouldn't deserve the name "screen" if it wouldn't have a solution to it. Just as in vim, there are certain keyboard combos that are not well known but completely change the meaning of the program. In this case screen can be told to switch to the scroll mode. This can be achieved by using the following keyboard combination: [CTRL]+[a] followed by the "[" (square bracket) key. Depending on your keyboard layout, you may need to use the [AltGr] key to use the "[" key on the keyboard.
Scrolling up with the mouse now works, and so do the [Page Up] and [Page Down] keys.
This way the whole screen session can be seen from begin to bottom once again.
GosuJunk from wrote on Mar 29th, 2024:
It is 1am at the moment and I love you very much for this wonderful blog article.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder