Ah, the struggle of stepping away from the computer for a quick coffee break, only to return to a disconnected terminal. If you are frustrated by re-ssh-ing, navigating to the right folder, and scouring through command history, then you should learn about Linux command screen  through the following exercises


Exercise 1

Start a screen session, detach the session, and reattach it. It is assumed that you will complete these tasks after logging into a server.

  1. Begin your session and give it a name by typing screen -S TestSession in the terminal. If a message appears, simply hit the “enter” key. Now, you have successfully entered a virtual session.
  2. Execute the top command to display all the active processes on the machine.
  3. Detach the screen by typing ctrl a d together (i.e. press and hold down the Ctrl key, then press A and continue to hold it down. Finally, press D).
    You are now back in the terminal session where you started prior to launching screen.
  4. To reconnect to the “TestSession” screen session, simply enter screen -R TestSession in the terminal and you will find that the top command is still in operation, appearing as though the session was never terminated.

Exercise 2

Exit a screen session. Having completed the previous exercise, you are now aware that the TestSession session is running in the background on your computer. To learn how to end it correctly, or kill it, you should attempt this exercise.

  1. Reattach to a session using screen -R TestSession
  2. Exit the screen by typing ctrl a k together (i.e. press and hold down the Ctrl key, then press A and continue to hold it down. Finally, press K).
  3. Now if you try to reconnect again by entering screen -R TestSession you will be taken to a new session!

Exercise 3

The full potential of screens is revealed when using multiple terminals within a single session. This is particularly useful when accessing a server and opening various folders or executing multiple commands while keeping the terminals open. After opening a new screen session or reattaching to an exiting one, try the following

  1. Type ctrl a c to open a new window (i.e.first press and hold the Ctrl key, then press A while still holding down Ctrl. Finally, press C.)
  2. If you repeat the same command again, it will open another window and take you there.
  3. To toggle to the next window (following a cyclic order), type crtl a n.
  4. If you want to go to the previous window (i.e. following a reverse cyclic order), type ctrl a p.

Exercise 4

Another great advantage of using screen is that you can use multiple screen sessions; unlike in the previous exercise, where you opened multiple terminals in one screen session. You can list all the opened sessions and reattach to any one of them.

  1. Open a session named “TestSession1” and detach from it.
  2. Open another session named “TestSession2” and detach from it.
  3. Now you are back to your the-real-terminal session. Type  screen -ls to see the list of sessions that are active.
  4. Your output should look something like thisraghurama$ screen -ls There are screens on: 97253.TestSession2 (Detached) 97249.TestSession1 (Detached)
  5. If you want to connect (i.e. reattach) to one of the sessions, type screen -S 97249.TestSession1, where I have also included the process ID (the number in front of the session name). You can also use the screen session name without the process ID, screen -S TestSession1

Exercise 5

One of the slightly inconvenient aspects of the screen command is that once your display is full, it doesn’t allow direct scrolling using the mouse or up/down arrows. However, there is a solution. Let’s explore how to navigate the screen in this exercise.

  1. Open a session and print a lot of lines. For example, in a new screen session, type seq 1 1000
  2. Now try to scroll up, and you will find that only previously used commands will be displayed.
  3. Type ctrl a Esc, and then you will be able to use the up/down buttons to scroll the page up/down. To escape from the scroll mode, you have to hit Esc again.
  4. Another option is to type ctrl a [ which also enables scrolling. But now, one has to type ctrl u/d for moving up/down, and ctrl f/b for page up/down.

Having the ability to scroll within a screen session proves to be very useful when reviewing the history of commands previously executed in that session.