If you’re looking for technology, many people don’t know about screens, great gadgets or “terminal multiplexers”. I firmly believe that, for whatever reason, everyone using the terminal should store it in the armory. Very useful
The screen makes multitasking easy in the terminal. With it, you can run many tools in parallel in their sessions. After running something in its own “screen”, you are free to detach and reattach to it. Being able to “let things run in separate sessions” allows you to switch between tools.
If you handle a lot of tasks in the terminal every day, then you either already use it or you will love it after trying it!
The screen can be used on almost every distribution under the sun-because it’s an old, tried and tested, albeit little-known, little fortune. To install it on Debian, Ubuntu, Mint or anything using apt, use:
sudo apt install screen
Since it’s small and doesn’t have many dependencies, you can get up and running in seconds.
We will only study the basic functions of the screen, which will allow you to use it immediately. If you liked what you saw at the end of this tutorial, its man page will cover these extra features, but we think they are too many for the first introduction to using it.
To use it, just add it before everything you type in the terminal. Let’s create a document using the popular nano editor as an example. If it is not installed, you can find it on your distribution, or if you prefer something else, select “nano”.
screen nano mte_screen.txt
By adding “screen” in front of the regular command, we can run it in one of the sessions of the screen. It seems to have no results, but as we will see in the next step.
Leave the meeting
Type in nano, then press Ctrl + A, then D. Nanoº on the keyboard or any editor you use will disappear. In the terminal, you will see a message similar to the following:
[detached from terminal-ID]
You will now return to the starting terminal from which you ran the screen. But your conversation with nano did not disappear.
Back to running session
Because you run the session in the background, you can return to the session by entering:
After pressing Enter, you will enter where you left off in the nano. You can detach and reconnect the screen as many times as you like. But this is just an application running in the background, far from true multitasking.
Create a second session
When you return to the original terminal and leave the running nano session, repeat the first step to run something in the second session. For simplicity, we created a second document using nano:
screen nano mte_2nd.txt
However, the screen is not limited to multiple sessions running the same application: try to use it to run anything. Screen proved its usefulness when using processes such as 7z to compress many files into archive files, and the process took some time. You don’t have to stare at the terminal and wait for 7z to end, you can detach its session and let it run in the background.
If you followed our previous steps, you can now run two sessions on the screen.
screen -r Won’t work as before, because it doesn’t know where to attach. When you have multiple sessions, to return to one of them, you must first know its ID. To find it, enter:
The screen displays a list of all available sessions.
For our part, as you can see in the screenshot, to return to any session, we have to enter:
screen -r 14384
screen -r 14336
Useful additional features
If your running task exits after completing a screen operation, the screen session ends there. This is why you can run some tasks in a screen session, but no active session is displayed now. These tasks can accomplish their goals at the same time.
If you want the session to remain active in this case instead of using
screen Before adding, run the Screen yourself to create a new session, and then type commands in it before detaching. If you run the command in a manually created session, the session will not exit after the task is completed.
The screen also allows you to create a new session from an existing session. Just press the command key combination Ctrl + A and then press C to create a new screen and jump to that screen.
For other useful commands you might end up using, press Ctrl + A, then:
- Enter a conversation title for easier identification and management
- K kills the current session
- N or P move to next or previous active session
- 0 to 9 move between the first ten active sessions
Not only for juggling tasks
We save the best as the last: the screen is not bound to a specific terminal. After detaching from the session, if you are in a graphical environment, you can close the terminal window and even log out. As long as your computer is running, the session will remain active.
This means that you can reconnect to a running session from another terminal. As you can see in the screenshot, we use Guake to reconnect to the session created in Mint’s default terminal.
As the impact of this situation may not be fully understood, consider the following use case: You can use SSH to log in to the computer remotely and then launch tasks from the screen. Then, detach and log off. The job will run until it finishes or you decide to log in again, reconnect to the screen session and end manually!
What about “multitasking”?