Screen Synopsis

Discussion in 'Shell / Command Line' started by Eric Hansen, Oct 17, 2013.

  1. Eric Hansen

    Eric Hansen Moderator Staff Writer

    Messages:
    124
    Likes Received:
    87
    Trophy Points:
    28
    Most people already have this handy little program installed but are unaware of it since its not one to be promoted very much. However, if you’re one to work a lot from the terminal like me you quickly realize how much of a life saver it is!


    Screen can be thought of as a tabbed terminal window in that it allows you to have multiple windows or sessions available at any given time. I’ve known some people to run a different program (IRC, email, chat, browser, etc…) in their own screen window. I have 3 different SSH sessions running in their own window, even, and it makes management A LOT easier.

    The way to do this is quite simple. Once you have screen installed just type in:
    Code:
    screen -dmS (session name)
    While there’s various useful switches, “dmS” combines some very nice features (detach, create a new window and name the screen session to “(session name)”) that help get you up and running. If you excluded these then screen would take over your current terminal session. Just replace “(session name)” with a unique name (mine are the SSH hosts they are monitoring) and you’re good to go!

    How does this help manage multiple servers, however? Well, screen loads up a new pseudo-terminal (thus why it uses /dev/pty/0 by default) and loads in the user’s profile who started screen. This helps to ensure anything you could do otherwise can be done in screen sessions. For me it made it easy to just type “ssh hosting” and leave the screen window be.

    To detach from a screen window (return back to the original terminal but keep a screen window running in the background) just do ctrl+a then d. This will detach the screen and still keep it active.

    If you use LXC or any other program that requires commands via ctrl+a then there’s an additional key that needs to be pressed: ctrl+a then a then d. This is slightly annoying but if it annoys you too much you can change the command keys by passing “-e xy” to the screen command. You need to replace “x” with the key that will tell screen you’re sending a command, and “y” with the key that tells screen you’re sending a literal command. So in the default case it would be “-e Aa”.

    Using screen in this manner does also offer a bit of a security advantage too. If your network dies in the middle of an update you don’t have to worry about missing what happened during as screen will be keeping those sessions alive. You can re-attach to a session at any time by running:
    Code:
    screen -r (session name)
    If you’re working on multiple servers then creating multiple screen sessions will allow you to have the benefits of a tabbed terminal without needing a GUI, which for CLI enthusiasts like me is a godsend. This can also improve debugging time as you don’t have to keep disconnecting from SSH to reconnect to another server. A perfect example is trying to get site-to-site VPN to communicate with each other. The time saved from having to constantly disconnect/reconnect via SSH can be highly valuable to both the VPN network and your job.

    Attached Files:

Share This Page