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 […]
  • Globally disable directory listing on WHM/cPanelGlobally disable directory listing on WHM/cPanel One of the things that WHM/cPanel servers have on by default is directory listing. This is something you don’t want – but it’s easy to fix!

    1. Log into WHM
    2. Apache Configuration […]

  • Download CentOS 7 Nightly BuildsDownload CentOS 7 Nightly Builds As the CentOS folks are working on releasing a final version of CentOS 7, you can get in on the action and download their nightly builds. We’re estimating (guessing? Based on previous […]
  • Run the same command on many Linux servers at onceRun the same command on many Linux servers at once Ever have to check a list of Linux servers for various things like what version of CentOS they’re running, maybe how long each has been running to get an uptime report? You can – and it’s […]
  • Migrate WHM/cPanel from one server to a new one (script)!Migrate WHM/cPanel from one server to a new one (script)! We’ve started a site to house a script that will migrate your full WHM/cPanel server to new hosting. This takes the new WHM/cPanel backup files and will restore them including system […]

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=""> 

*