Using GNU SCREEN when working in Terminal

What is GNU SCREEN

GNU SCREEN (screen) is a text-based program usually described as a window manager or terminal multiplexer. While it does a great many things, its two biggest features are its detachability and its multiplexing. The detachability means that you can run programs from within screen, detach and log out, then log in later, reattach, and the programs will still be there. The multiplexing means that you can have multiple programs running within a single screen session, each within its own window. You can display multiple windows at once, or just switch between them, even if you only have a single terminal window to use.

Benefits of using screen

If your local computer crashes or you lose the connection, the processes or login sessions you establish through screen don’t go away. The screen program creates multiple processes instead of multiple Unix login sessions, which means that it is resource-efficient. Using the detach feature, you can save screen processes when logging out and resume where you left off, saving the trouble of restarting them.

Why not just use ‘&’ or CronTab?

Running a MySQL query with ‘&’ alone does not guarantee your query will not be interrupted. You will have to use nohup (ignore the hangup signal). From our experience, this did not always work. Screen provides an elegant and user-friendly way to run multiple queries without having to fear it being interrupted or not being able to return to it at any time.

CronTab does not let us specify a year. This means if you forget to remove an entry it will run the next year again, automatically, even if you are not with us any longer! It also does not allow us to kick it off manually.

When should I use screen

We would recommend you always you screen when working in PuTTY (Terminal). Have a screen dedicated as your sandbox environment and create additional screens as required for long running queries or any other use case you might encounter.

NOTE: Do not use background processes (&). When you detach from your screen it will run in the background and you can start a new screen. dsd

How to use screen

  1. SSH into your server as you normally would.
  2. Type screen -dR "your username" to create a named screen for you to work in. If you type screen -ls you will now see you have a socket like this:

    PL-MS-i5-2019# screen -ls
    There is a screen on:
        45.pieter     (02/08/19 14:01:16)     (Attached)
    1 Socket in /run/screen/S-root.

    Here I can see I have a Socket named 45.pieter and I am currently connected to it since it is Attached.

  3. Now you can run your commands as you normally would. For Example:

    mysql my_test_script.sql > output.log 2> err.log
  4. To detach from a session and let it run in the background press Ctrl+a this tells screen you will be inputting a command and then press d to detach from this session. You should see a message like: [detached from 45.pieter] You are now free to close your terminal. You will see this socket is now detached when you run screen -ls.

    PL-MS-i5-2019# screen -ls
    There is a screen on:
        137.pieter      (02/08/19 15:24:35)     (Detached)
    1 Socket in /run/screen/S-root.
  5. To get your screen back you need to reattach to it with screen -dR or screen -dR pieter if you need to connect to a specific one.

You can close and terminate any screen you are attached to and working in at any time by pressing Ctrl+a and then typing :quit. A short way is simply typing exit but you do carry the risk of exiting PuTTY completely.

If you close your ssh connection without detaching or lose your network connection: run screen -dR <screen name> The -dR means “find my screen session, detach it from wherever it’s attached if it’s attached, and reattach it here, if I don’t already have a session, create one”

Quick Reference

Description Command
Start New Screen screen -dR “your username/screen name”
Create New Window Ctrl-a c
List your screens screen -ls
Exit Screen exit / Ctrl+a :quit
Detach Screen Ctrl-a d
Re-attach Screen screen -x or screen -x PID
Split Horizontally Ctrl-a S
Split Vertically Ctrl-a |
Move Between Windows Ctrl-a Tab
Name Session Ctrl-a A
Log Session Ctrl-a H
Note Session Ctrl-a h

To force quit a screen session: screen -S <sessionname> -p 0 -X quit

For a full Quick Reference list see: SCREEN Quick Reference