HomeAWSAzureGCPIBMToolsAboutcontact Us

Screen 101: Use Screen to Manage SSH Sessions

Posted On March 15th, 2018

Discussing Tools

If you’re not a Linux power-user then you probably have never heard of Screen, in fact, I would hazard to say most Linux users probably have never heard of this utility. You don’t need to be a Linux desktop user (I’m not) to find Screen useful. I typically use it when running a script that will take hours or days to complete and I can’t risk the script being killed if I lose my SSH session to the server. You may not find this useful if you simply use Linux in a web hosting environment for your site or blog. However, this utility can be extremely helpful when you are editing a file (say a WordPress config file or your theme template) and don’t want to risk losing your work and breaking your site should your Internet connection drop. If, like me, you depend on Linux based servers to help you complete your day-to-day work then read on. In this first article I’ll discuss some basic usage of screen and then in a follow-up I’ll show you how to configure it to make it easier and more convenient to use. Even casual Linux or Mac OSX users who want to learn a great utility should continue. Screen will create a virtual shell session and keep your application running in the background even if you get disconnected. This is especially helpful for me when I leave the office and want to continue to monitor my work from home. I simply login to the server via SSH and reconnect to that particular screen or virtual terminal.
$ ssh demouser@exampledomain.gdn
demouser@exampledomain.gdn's password:
Last login: Fri Aug 01 01:01:01 2011 from xxx-xxx-xxx-xxx.serviceprovider.net
[demouser@myserver ~]$ screen
  Now, with a standard configuration, your terminal window will go blank and you will simply have a command prompt (or the screen copyright notice) waiting for you. At this time you have a virtual terminal open within your normal terminal window. The easiest way to understand the power of this utility and to see what is happening with ping:
[demouser@myserver ~]$ ping google.com
PING google.com (74.125.93.104) 56(84) bytes of data.
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=0 ttl=51 time=11.6 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=1 ttl=51 time=11.7 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=2 ttl=51 time=11.6 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=3 ttl=51 time=11.7 ms
  Now, just simply close your terminal window. Don’t cancel the ping command or logout, just close the window as if you lost your Internet connection. Then log back in to your SSH account. Believe it or not, your virtual screen session(s) are still running in the background running the command or program you launched before. If that work completed the screen will be waiting with a command prompt ready to do more work for you. Typically you need just execute the screen command again with a -x or -r to resume your screen session. Note that -x is meant to be used when you have only a single screen session to attach to and -r is meant to be used when you have multiple sessions running and need to specify which you want to reattach. However, both work interchangeably and I've always only used -r so that's what your going to see me use:
[demouser@myserver ~]$ screen -r
  However, there are two scenarios that will cause you to get an error message. First, if screen has not yet realized that your original ssh session was interrupted, that screen will still be “attached” to the previous terminal connection. The message will look like:
[demouser@myserver ~]$ screen -r
There is a screen on:
     7316.pts-0.myserver     (Attached)
There is no screen to be resumed.
  Screen won’t let you jump right back in to this virtual screen because it is still “Attached” to another terminal or session. You can, however, force screen to Detach from the former terminal by adding a -D to the mix:
[demouser@myserver ~]$ screen -D -r
  The second instance that will generate an error is if there is more than one screen active at the same time. In that case, you will see a message similar to:
[demouser@myserver ~]$ screen -r
There are screens on:
     5910.pts-2.myserver      (Attached)
     7316.pts-0.myserver      (Detached)
There is no screen to be resumed.
  If you’d like to avoid this from the beginning you can ask screen to list your active sessions with the -ls flag. If you are familiar with Linux you will recognize “ls” as the command to list the contents of a directory or folder:
[demouser@myserver ~]$ screen -ls
There are screens on:
     5910.pts-2.myserver     (Attached)
     7316.pts-0.myserver     (Detached)
2 Sockets in /var/run/screen/S-myserver.
  When there are more than one screens active on the server you need to specify which one you want to connect to. You do this using the number at the beginning of the line describing the sessions and you will use the same flags as above to disconnect the attached terminal (if needed) and reconnect that screen to your current terminal window:
[demouser@myserver ~]$ screen -D -r 5910
  Now, continuing from the example we started above, when you reconnect to your screen you will see your command, in this case the ping command, is still running and was the whole time:
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=41 ttl=51 time=11.7 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=42 ttl=51 time=11.7 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=43 ttl=51 time=11.7 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=44 ttl=51 time=11.8 ms
64 bytes from qw-in-f104.1e100.net (74.125.93.104): icmp_seq=45 ttl=51 time=11.7 ms
  To stop your ping press C-c (Ctrl-c). When you are done you simply exit from the screen like you would any other terminal window or shell session:
[demouser@myserver ~]$ exit
[screen is terminating]
  In part 2 of this article, I will show you how to modify your settings for GNU Screen to make it easier to use and more convenient. Do you use screen? Love it? Hate it? Or have another utility you can’t live without? Let me know by leaving a comment!
Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2018 - Lane Vance - All Rights Reserved. Logos and Trademarks copyright their respective owners.
HomeAWS
AzureGCPIBMToolsAboutContact Us
facebook-squaretwitterfacebooklinkedinmenu