Using the Linux Screen Program

in linux •  7 years ago 

screen.png Bash Terminal (Konsole)

It's not uncommon for people to need to run commands in the Linux shell, whether on a local box or on a remote machine that you've SSHed into. Often we need these running programs to persist while we go and do other things. You can, of course, detach the process by putting an ampersand (&) after the command, but then we often can't see the outputs or interact if needed.

If we're on a local machine we can probably start a new shell and do stuff in there, but if we've used SSH then things aren't necessarily as clear-cut. Ideally we don't want to keep our SSH connection open for the entirety of the time the program is running just to keep it going, instead we want to be able to log in, start things running, log out, go and do other stuff, then log back in later and see the outputs from our program, or other information which is important.

This is where screen comes in. At it's most basic, which is all we're going to cover here, this is what screen allows you to do. We can do similar things on the local machine, and it's recommended that you try these out locally before attempting them over SSH. Assuming that we're working on a remote machine do the following:

  1. Log in via SSH

  2. Start the screen program by typing screen at the prompt.

    some-console>$ screen
    
  3. If screen is already installed you'll get a license blurb to read through, before it drops you into what appears to be a new shell session (if it's not then see below). Now run some commands as normal. Possibly start an ongoing process such as top.

  4. Now we are going to detach our shell from the screen session. Do this by typing CTRL-"a" (Hold the control key down and press the "a" key at the same time - this is how we trigger screen commands) then press the "d" key (d for detach). You should see your shell revert to the previous prompt, possibly with a message that says something like:

    [detached from 10525.pts-3.amos-ThinkPad-T430]
    

    (That's a message on my laptop when I detach from a session)

  5. The session, possibly containing a running instance of top is still there, we just can't see it at this point. To reattach our shell to a running screen session, we type screen -r at the prompt (this can even be inside a completely different shell window if we're working on the local machine). If you're using SSH then you can log out, then log back in and it will still work.

    some-console>$ screen -r
    

    As if by magic our previous screen session will reappear (you'll see top once more if that's what you left running).

  6. Once you're happy doing this on your local machine, then try doing it over SSH on a machine you have control over. This is potentially one of the most useful commands you'll learn to use in conjunction with SSH as it allows you to persist your programs between logins.

[NOTE: If screen is not installed then you'll need to install it first using your package manager of choice (yum, pacman, apt-get, slapt-get or whatever your distribution prefers). This is probably as easy as doing sudo apt install screen on a Debian based distro. If not then you'll have to investigate how to do that, but that's beyond the scope of this article.]

To find out more about what the screen command can do then try typing man screen in the shell to bring up the man page which has all of the options and details about it. I've barely scratched the surface here.

man-page.png
Man Page for the screen command

There are other similar programs out there, such as tmux which I haven't used, but I may go and investigate now, and write a similar tutorial about.


My name is Dave Ames. I've been a teacher for 25 years and for the last few of those I've been teaching both children and other adults, especially teachers to program in a variety of environments, but mostly Python.

This is one of an occasional series of posts that I'm going to make about useful tools that live on the fringes of programming and using Linux in general.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Great intro to screen. I personally prefer tmux, and having gone through that changeover myself having to relearn the key buininds for for some of my more commonly used actions was a pain. Muscle memory can be hard to unlearn.

I used screen to connect to serial ports from Linux. It seemed a little more transparent than using minicom. It never occurred to me to use the screen's other features.

Then I saw a YouTube video about tmux. I'm careful to add new tools to my everyday workflow, but in only a few days I was relying on it heavily.

Fab. I might have to go and investigate tmux then. Plus I'll maybe write an introductory post about it too.

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly – Featured Posts are voted every 2.4hrs
Join the Curation Team Here