HowTo: RaspberryPI - Multitasking and "I'll do that later" with Screen

Working in the console has a few disadvantages that are not immediately obvious. For example, in Windows you are used to being able to switch between windows.
At first glance, simply “hopping” into the browser, copying a link and back into a Word document that is open at the same time to paste the link there is not possible in the console.

Connection problems can sometimes have annoying consequences:
For example, if your RasPi is somewhere in the middle of nowhere and does its job as a lonely weather station with a very sporadic network connection, it can happen that your console connection is interrupted. This also means that your console connection will be interrupted and all processes you have started on the RasPi will also be terminated.

If you have just run a process that does something important for 15 minutes and now the connection has been lost for the third time after 14 minutes, one or the other will slowly start chewing on the tabletop in anger.

In order to protect your teeth and simplify some work processes, the “Screen” program was invented.

This allows you to launch multiple virtual consoles in one console session, switch between them and continue even after your connection has been interrupted and restored.


Safety instructions

I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/

Affiliate links/advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂 


Overview

Before you can start installing Screen, you should have prepared the Raspberry Pi so that it can be accessed via the network and via SSH.
The following three articles describe what needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPI - The First Configuration!
RaspberryPi – Control the RaspberryPi via SSH

Required tool:
-no-

Required material:

In the following list you will find all the parts you need to implement this article.


Log in to the RaspberryPi via SSH

To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.

After entering your username and password you can enter the first commands.

Update package management

The package management in Linux is a "central place" through which various software packages can be installed. In order for this to work reliably, the lists and sources of the package management should be updated before each installation of new packages.

To start the update of the package management you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long ago your last update of the package management was, this process can now take a while. The lists that refer to the individual package sources are updated first.
After that, the packages themselves are updated. Since additional memory is occupied, you will be asked again for your consent. You have to confirm this with a "J" and "Enter".
Once the update is complete, you will see a small summary of the duration and scope of the update.

Install screen

You start the installation of Screen with the following command:

sudo apt-get install screen

During the installation of the updates and the subsequent packages, you may be asked whether you agree that the modules to be installed will take up additional storage space. You confirm these questions with a “j” and Enter. The installation will then continue.


Start a virtual console with Screen

Screen is now installed and you want to start your first virtual console. This Virtual Console will continue to run even if your real console is closed. To start a virtual console from the real console you have to enter the following command:

screen bash

You will then find yourself in a new/fresh virtual console environment that is visually no different from a real console environment. You can work in this just like in the real console environment.

But if you have a process that runs for a very long time, once you have started it in a virtual console, you can move it to the background by pressing CTRL+A+D. (So you first press CTRL, hold down this key and then press “A” and then “D”)

The virtual console then disappears and you are back in the real console. You can now close these or do something else. Your Virtual Console input and processes will continue to be saved and run in the background.

You could now also start another virtual console and let it run parallel to the previously started virtual console.


Show all screen instances

In order to switch from the real console back to the virtual console, you should first see which virtual consoles are currently active. This can be done with the following command:

screen -list

After you have executed this command, all active virtual consoles will be displayed.

In this case, two virtual consoles are currently active. The upper one has the name “1435.pts-0.magicMirror”, was started on November 15th, 2017 at 3:59:40 p.m. and is currently “detached” (= separated) i.e. not connected to the real console.

The lower virtual console has the name “1413.pts-0.magicMirror”, was started on November 15th, 2017 at 3:50:35 p.m. and is currently “detached” (= separated) i.e. not directly connected to the real console.


Connect to an existing Screen instance

In order to display a virtual console again so that you can work in it, there are basically two options.

1st option

“screen –list” only shows you a virtual console active in the background:
In this case the following command is sufficient:

screen -r

This will cause your real console to be connected to the only available virtual console.


2nd option

“screen –list” shows you several virtual consoles active in the background:

In this case, in addition to the “screen –r” command, you must also specify the name of the virtual console you want to connect to.

For example, with “screen –list” (as can be seen in the picture) you will see two virtual consoles with the names “1435.pts-0.magicMirror” and “1413.pts-0.magicMirror” and you want the virtual console with the name “1435.pts-0.magicMirror” you have to enter the following command:

screen –r 1435.pts-0.magicMirror

Pro tip:
In this case, “screen -r 1435” would be sufficient because it clearly indicates which virtual console should be connected.


End a screen instance

There are two ways to end an active virtual console.

1st option:

You connect to the virtual console that you want to close and press CTRL+D. This closes the currently connected Virtual Console without any further warnings. Alternatively, you can also enter “exit”. This also ends the active virtual console.


2nd option

You are in the real console. Similar to reconnecting to an existing Screen instance, you can also terminate one.

Let's say you want to close the virtual console named “importantVirtualConsole”. Then you have to enter the following command:

screen -X -S importantVirtualConsole quit

This will stop the virtual console running in the background without you having to connect to it first.

So you now know how to use “screen” to create and manage various virtual consoles on the RasPi.


Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at ko-fi.com       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

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