Data Centers

How to use the Linux screen command to keep your remote processes running

Jack Wallen shows you a handy Linux trick that can go a long way to prevent you from losing precious command line work due to questionable network connections to your remote servers.

linuxhero.jpg

How many times have you secure shelled into your data center Linux servers, only to have (for whatever reason) your remote secure shell connection broken? When you reconnect to that server, is that critical command you issued still running? Or did the severed connection put the kibosh on the command? Not only can this be incredibly confusing, it can also be a cause for you to lose precious work and time.

Fortunately, there's a way around this: The screen command. With screen, you can reconnect to that server and pick up where you left off with your last command. I want to show you how to make use of this Linux admin tool. I'll be demonstrating on Ubuntu Server 16.04, but screen is available for nearly all Linux distributions.

What is screen? Simply put, screen is a full-screen window manager that multiplexes a physical terminal between several processes. When you call the screen command, it creates a single window where you can work as normal. You can open as many screens as you need, switch between them, detach them, list them, and reconnect to them.

Let's walk through the process of using this handy command.

Installation

On Ubuntu Server 16.04, the screen tool is already installed. If you find it's not there, you can install it with the command sudo apt install screen. If you're using Fedora, that install command would be sudo dnf install screen. That's all there is to installation.

Usage

Secure Shell to your remote Linux server. Before you start working, issue the command screen. You will be greeted by a welcome window (Figure A).

Figure A

Figure A

The screen welcome window.


You can customize that screen to make it easier to remember what screen is running what command, like so:

screen -S NAME_OF_COMMAND

Where NAME_OF_COMMAND is the actual command you'd run.

Hit either the Spacebar or Enter to exit the welcome window. You will now find yourself at what looks like a standard prompt. Let's issue the command sudo tail -f /var/log/syslog. The tail command will start following input on the syslog log file (as expected). Now let's open a new screen by hitting the [Ctrl]+[a]+[c] key combination. This will give you a new prompt. Now let's ping our server with the command ping 192.168.1.208 (insert the IP address of your server). With that ping running, we can switch back to the previous screen with the key combination [Ctrl]+[a]+[p] (which switches to the previous screen). You should now be back to your sudo tail -f /var/log/syslog screen. Hit the [Ctrl]+[a]+[n] (which switches to the next screen), and you're back to the ping command.

But what happens when you lose your connection? Are your commands lost? No. Issue the command screen -ls and you'll see your two screens listed (Figure B).

Figure B

Figure B

Our currently running screens.


Each of those screens has a session ID, which allows you to reconnect. So our session IDs are:

  • 2216.pts-0.ubuntu16
  • 2067.pts-0.ubuntu16

You can reconnect to one of those screens with the command like so:

screen 2216.pts-0.ubuntu16

The one issue we might have is that the only screens you can reconnect to are those listed as Disconnected. This will happen if screen detects a network disconnection. If you become disconnected, and the screens haven't detached, you can detach and reconnect with them manually with the command:

screen -rd 2216.pts-0.ubuntu16

Or, say you initially started a screen with the command screen -S kernel-compile. You can reconnect to that screen with the command:

screen ID.kernel-compile

Where ID is the 4-digit ID number.

If that screen hasn't detached (Figure C), you can reconnect with the command:

screen -rd ID.kernel-compile

Where ID is the screen ID number.

Figure C

Figure C

Our kernel-compile screen listed as attached.


If you need to step away from the terminal, but want the command to continue running, you can manually detach the screen to reconnect with it later with the key combination [Ctrl]+[a]+[d]. This will return you to the standard prompt.

Finally, to exit a screen, simply type the exit command and you're back to the standard bash prompt. Make sure, when you're done with your screens, to reconnect to them, end their commands, and exit.

A must-use command

If you frequently remote into your Linux servers, and you want to make sure to not lose your command line work due to a questionable network connection, screen is a must-use command. To learn more about what this can do, issue the command man screen and read through the manual page.

Also See

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox