It may sound bold to think that a simple Linux utility can be life changing. In a world of remote access and remote administration this nifty little program just got a whole lot more valuable. How often have you found that you want to run some long-running task on a remote server and your connection fails because of a timeout or just forgetting you had a remote session open? (hint: it happens to me a lot).
Imagine that you have an upgrade process that requires a shell script or some batch process to run in an online console or may have a tracing process that is needed to troubleshoot an active task or server that is running.
There are lots of use-cases that need an active shell session to be running on a remote server. This is where screen will become your best friend!
We GNU You Would Enjoy This Utility
The screen (often called GNU screen) is found already installed on many Linux platforms already. It’s also super easy to download even if it isn’t already present. CentOS may need you to install the EPEL repository first, but Ubuntu should be straightforward, so these are the commands to install screen:
sudo yum install epel-release -y
sudo yum install screen -y
apt-get install screen -y
Now you’re ready. Let’s get started!
Starting your Screen Session
The purpose of screen is to create a virtual shell that is assigned a PID (process ID) and is able to be detached and left persistent and active in the background. Very cool! All you need to do is type the command screen and you will enter your first session.
You can kick off some process for fun to show how it works. I’ll try a simple ping command which will keep running for us as we jump in and out.
Detaching from the session is done using the key combination Ctrl-A+D (Ctrl/Control key, the A key and the D key all at the same time). You’ll see the result as it notifies you
Now you have your session saved and running as a background process.
That brings up your list which will show the active session:
Reattaching to your screen session is done using the screen -r command. It really is just that easy!
You’re back in the active session and can either exit out or do the same Ctrl-A+D to detach and leave it running just like you did before.
Running Multiple Screen Sessions
You have much more potential because you can even run more than one screen session on the same server. Each session will get its own PID so you just have to spin one up and detach and then repeat the process again.
Start up your first session using screen and then detach using Ctrl-A+D. Now do the same thing again using screen and then detach using Ctrl-A+D. You can check the screen sessions using the screen -ls command:
You are running two active sessions in the background and this means you can choose which one to reattach to easily using the PID that shows up when you do your screen -ls command. Reattaching to a specific session uses the -r and the PID:
screen -r <pid>
That’s super handy but also relies on you keeping track of which process you have running in each screen session. What if we could take it just a little further and label your sessions?
Naming Your Screen Sessions
Adding a simple -S switch to the command and a nickname for your process means that your new screen session will be spawned and have the session name for easy organization.
screen -S disco
Using the screen -ls command shows the background session with your session name which you now reattach to using the screen -r <sessionname> command:
screen -r disco
How cool is that?! Being able to leave background tasks working away while you disconnect is a huge win as you probably have less time at a physical console lately. Running background tasks on cloud instances definitely better with short term connections, using screen, and then reconnecting rather than leaving an active SSH to the live console and risking losing your connection which could have damaging effects on the running process if it drops.
There are some other nifty commands that you can explore using the screen -? command which is actually not a proper command argument but will bring up the help selection for it. It’s a little odd that there is no specific help flag but if you have the man pages installed there is a full manual entry for it.
You may have noticed that the challenge is leaving sessions running in the background and forgetting them. There are always cleanup options to remove all background processes, but I tend to lean towards reattaching to each one and confirming there are no active processes and then using the exit command to close out the shell.
We recently did a full Turbonomic Labs session that explored this whole set of commands and more in an interactive live stream which you can see on YouTube below. Hopefully you find the magic of screen as helpful as we do!