1. Overview

Sometimes when working on a Linux machine, it can be hard to keep track of tasks split across several terminal windows. It’s also time-consuming to set terminal sessions back up after closing the windows.

In this quick tutorial, we’ll learn how to use GNU screen to maximize workspace efficiency and keep a continuous setup at each login.

2. Installation

screen should already be installed on most standard Linux distributions. If it’s not, a package installer will do the trick — for example:

$ yum install screen

or

$ apt-get install screen

We can also download the source code and install it manually.

3. Building a Session

3.1. Starting screen

First, we’ll start a screen session in a Linux terminal window:

$ screen

By default, there’s one window in the session, and it presents a command prompt in the directory in which we ran the screen command. A window in screen is similar to a tab in an Internet browser — there can be one or more per session.

We can see the current list of screen windows by hitting the keys:

CTRL+a "

We’ll see something like this in the terminal:

Num Name
  0 zsh

3.2. Renaming a screen Window

Naming a screen window after creating it is a good way to keep our screen session organized. Since we’re going to create several screen windows, let’s rename our current window to make it easier to find later on.

First, we hit:

CTRL+a SHIFT+a

Then a prompt will come up to let us rename the current window:

Set window's title to: zsh

Let’s change the title to logs and hit Enter. Next, let’s show the window list:

CTRL+a "

We now have a custom title:

Num Name
  0 logs

Now it’s time to add a second window to our session.

3.3. Multiple Windows

To create a second window in the screen session, we use:

CTRL+a c

After showing our window list with:

CTRL+a "

We’ll now see two windows in the session:

Num Name
  0 logs 
  1 zsh

Let’s rename this new window using:

CTRL+a SHIFT+a

We can change the name to timer. Now our list is:

Num Name 
  0 logs
  1 timer

We can switch windows in the window list by using up and down arrows or the j and k keys. The Enter key will make the selected window active.

As an alternative to the window list, we can use key commands to navigate windows.

Let’s try them out. We can move to the next window by using:

CTRL+a n

We can move back to the previous window by using:

CTRL+a p

Finally, we can toggle to the previously active window by using:

CTRL+a CTRL+a

3.4. Running Simultaneous Commands in Different Windows

Now let’s see how we can have a command running in one window, and work alongside it in a second window, all within the same screen session.

We’ll first navigate to our timer window:

CTRL+a "

Depending on our current window, we’ll need either the up or down arrow to get to the timer window. Enter will select it.

Let’s start a simple timer command to write to a file:

$ echo Starting at $(date) > counter.txt; \
  while true; do echo $(date); sleep 5; \
  done >> counter.txt

This will output the current date and time to a file every five seconds.

Now, if we go to the other window in the session using:

CTRL+a n

We can now start a tail command to monitor the output of our timing script:

$ tail -f counter.txt

This will begin outputting:

Starting at Sun May 31 14:36:30 EDT 2020
Sun May 31 14:36:30 EDT 2020
Sun May 31 14:36:35 EDT 2020
Sun May 31 14:36:40 EDT 2020

So now we’re running some code in one window and monitoring the output in a second one.

4. Reconnecting a screen Session

It took a bit of work to construct the screen session in the previous section. Instead of having to redo it when we come back to our machine, screen lets us detach from a session and reconnect to it at a later time. All of our windows, setup, and commands will be restored exactly as we left them.

Let’s try it out. To end our session, we can either close the terminal window or detach from our session using:

CTRL+a d

Our session stays active as a separate process. When we’re ready to keep working, we can reconnect to our previous screen session from the command prompt:

$ screen -R

The timer code should still be running, and the tail output should still be ticking.

As an aside, if we want to close a window in a session, we can just type exit in the window:

$ exit

This will close the current window and change to the previous window.

If we type exit in the last window in a session, the screen session will end with the message:

[screen is terminating]

5. Customizing a screen Session

We can customize a screen session by placing commands in a .screenrc file our user home directory. It’s also a way to easily port our preferred session settings to another machine.

As a quick example, let’s add a couple of customizations to a .screenrc file. In our home directory, we’ll first open the .screenrc file. Then we can add the lines:

screen -t logs
screen -t timer

After saving the file and starting screen:

$ screen

We can check the window list:

CTRL+a "

It shows the two named windows in the session:

Num Name
  0 logs
  1 timer

There are many more options in the .screenrc documentation.

6. Conclusion

In this article, we walked through using GNU screen to set up a reusable, customizable Linux terminal session with multiple windows.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments