Today we will be going over Screen. This is not the screen you are reading this on. No, it’s a remarkable terminal application that allows you to detach any session from its process to be later resumed. This is so that you may be more productive, running perhaps 10 jobs at once, rather than just messing around with one session at a time. You could be starting a job only to use a command to re-attach or perhaps toggle between screens to become a power user. Even better, you can split your screen to use all of your physical screen real estate. GNU Screen was created all the way back in 1987. Screen in written in C, so you know compatibility is not an issue. Screen is still maintained all the way until the time of this blog in 2022. That is some real dedication from GNU. Got to give them credit where credit is due.
We will cover how to install Screen on a few different systems, as it is so very useful. Next, we will cover how to set up processes to run in their own screen or region. Then we will show you how to detach. We will then show you how to recover a process. This is so useful when you need to run crazy scripts or just need to do more than one thing at once. The best thing is, you are not limited to one terminal or operating system. Let’s get into it!
Now we can do something a little counterintuitive. We can purposefully set a session loose on its own to do its work. This is not only to prevent a drop, but to also keep a 24-hour script from taking up your workstation session with it. You would need to install something like Screen as it does not come on distributions by default too often. We will install Screen on Debian, RHEL here as many other distributions are based on them. Therefore, they will share similar commands.
sudo apt install screen
sudo dnf install screen
Now we will start to show the main functionality that makes Screen such an outstanding tool to have in your tool box. If we want to assign a name to the screen as well. All you need to do is add it at the end. Just replace “assign_name” with perhaps the task you are planing to use the screen session for.
screen -S assign_name
You can also just type screen alternatively to just use a PID number rather than naming it.
This way you can just set a few screen sessions up fast without thinking too much about it. Just take note of the PID numbers. You can see all of your Screen sessions with a simple command like this.
If you want to toggle between two tasks, we can set up one screen for htop and another for neofetch.
screen && neofetch
Now we can set up htop similarly to get another screen running and to start htop.
screen && htop
To toggle between them, you can use the following key bind.
This way you can set a task and check on it intermittently. Saving you time trying to baby sit processes.
The next thing you might want to do is detach from a screen and go do something else. This is a really nice idea when your connection is not the greatest, and you just don’t know if you’re going to finish your task before your connection is disrupted. All we need to do is use the following key bind to detach from the current screen you are on.
Alternatively, we can use a command instead to detach.
The cool thing about this is you can just exit your SSH session and let your scripts run. Now, to close all windows and screens sessions you can use the following key bind Vim like command.
This way you can close out everything you’re looking at really fast. Freeing up time, so you can do some more jobs on some other server. You should still make sure to manage your other processes that are spawned in other screen sessions. To get all the key binds, check out the official documentation here. To get all the commands, check them out here.
Clearly we are going to want to re-attach to our screen sessions to make sure they finished or just to check an ongoing process. To achieve this, all we need to do is SSH back into the server. Once we have done this, we can go ahead and use this command.
You should be able to see all the sessions that you have running. Then all you have to do to attach to a particular session is run the same command with the PID number of the screen session you want, like this.
screen -r assign_name
This still feels like a magic trick every time I use it. If you have ever suffered a broken pipe or otherwise disconnected in session, you will see the value of Screen.
Now we may want some customization. You will need to make a rc file in your home folder to help you change Screen to suit your taste. We can bring out our old pal Vim to help us out. But first, we will cp over a copy of the exiting rc file in /etc/.
cp /etc/screenrc ~/.screenrc && vim ~/.screenrc
Now you can type a to edit and we can add a basic but fun configuration change and see how it turns out. Once you are done editing, hit the Esc key and then type :wq to save and quit the file. Then we can load our new rc configuration. Finally, we will need to source the file to use it. We will use another key bind combinations with a Vim like command.
Now we should see the changes we have made to our configurations. This will give you control over your own time and in a fashion that makes you happy. Why not set yourself up to automate your workflow? This is just the most amazing software to this author, considering how long it has been around just amazes me. Good job, GNU project. Please don’t hesitate to ask us any questions in the comments, and tell us how you like to use Screen. Thanks for reading.