How to use GNU Screen in Linux

Rob —  June 25, 2014 — Leave a comment

Screen is billed as a “full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)” (from the man page). Linux admins use screen for different purposes, but the most common is the ability to run long processes on remote servers – with the ability to disconnect, then reconnect later to see how things are going.

Other uses are to start a program/process then share it out, or allow another tech to sign in and take over. Whatever the reason to use screen, this little ‘howto’ will hopefully show you some of the basics and get you started.

Before we begin, there are other similar tools out there that provide the same (and in some cases, more) features. We’ll go into these in another article, but you could also check out tmux and byobu.

Screen Basics:
First, get into a shell on a Linux machine somewhere. At the prompt type screen:

Now, you’re in a screen! You can tell you’re in a screen if you echo your $STY:

Start up a program that will keep running until you kill it.. like top:

Ok, now let’s leave that running and detach the ‘screen’ so we can come back to it later on…

Use the keystroke: ctrl-a d

You should be back at your prompt again and top would be gone.. You may see something like:

Hey, the server seems slow.. let’s check out top and see what’s going on.. to re-connect to your screen session, type: screen -r

Now, you should be back in your screen again.

Ok, now quit out of top (ctrl-c) and let’s kill this screen with ctrl-a k. You’ll see something like:

Getting fancy:
Let’s get a little fancy.. you can think of a couple of things you’re going to use screen for.. maybe one is irc.. maybe another is a software build on a remote server.. so let’s open a couple screens and name them as we go.

Start a screen that is named ‘irc’:

Now, let’s detach it.. ctrl-a d

Start another screen called ‘build’

Let’s detach that one.. ctrl-a d.

We have two screens out there now. You can always see if there are any screens hanging out by listing them:

You should see something similar to:

Let’s attach to the irc one:

BOOM – you’re in your IRC screen again!

You could also create a new ‘window’ once in screen then cycle through them if you like.
Exit out of that screen (ctrl-a d to detach or ctrl-a k to kill it) and let’s start a new screen called ‘test’

Same as before.. you’re in a new screen called test. Let’s add a window to this screen session:

Now you’re running two windows in your screen session! Type something to get some output here.. maybe start top again.. once it’s going, switch to your other window:

Hit enter a couple times to see some output.. then switch back to your window running ‘top’ with ctrl-a n again..

You can also switch to a specific shell number.. for instance: ctrl-a 2, ctrl-a 1

Ok, that’s probably enough to get you started / in trouble. For more information on screen, you can visit the official GNU screen home: http://www.gnu.org/software/screen/.

Related posts from Linux Brigade!

  • Turn off unneeded services on your Linux machineTurn off unneeded services on your Linux machine One of the easiest ways to start securing your server (and speeding it up) is to turn off unnecessary services that are usually running by default. This little script will go through and […]
  • Add time and date to your bash historyAdd time and date to your bash history Often, you’ll go looking for a shell command that you used in the past – you can remember around when you did it but of course that won’t help you because by default the bash history won’t […]
  • LFD: Excessive resource usage If you run the CSF/LFD firewall on your server then you’ve no doubt seen the emails complaining about high resource usage for various accounts. This small tutorial will show you how to […]
  • Learn the CSF command line options You have CSF installed and use the GUI to do most things.  (If you don’t have it installed yet, you can read our article on how to install csf).  BUT – knowing the csf (ConfigServer […]
  • Install XenServer via usb flash driveInstall XenServer via usb flash drive USB Flash drives are getting cheaper and cheaper lately. I just picked up few at staples – 16GB flash drives for just under $8. I like to use them to install Linux – I always seem to […]

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url=""> 

*