I screen, you screen, we all screen when coding!

Angel Manzur
4 min readSep 12, 2019

--

Has this ever happen to you, or to a friend of a friend?: You are running a program from the command line that takes a long time to complete, and before it finishes, you close the window by mistake; or you are connected to a remote terminal running that last neural network model you need for your dissertation when you loose connection. In both cases, your session is lost and you will have to start all over again!

You can avoid a lot of frustrations and save some time by detaching your screen from the terminal using the screen command! This command is for Linux and Mac systems, for Windows systems, you will have to search how to enable a Linux Bash shell.

What screen does, is that it detaches the screen from the terminal. So if the window is closed, or the session gets disconnected, you can recover the screen. More important, whatever program you were running when the window closed, will keep running! (as long as the computer is still on).

Not sure what this means? Just follow these steps on your computer and see for yourself!

  1. Start a terminal and run the screen command

2. You will get a screen like the one below, hit Space

3. That is it, code away! Your session is no longer “attached” to the terminal. If you close the terminal (or loose connection if this terminal is in a remote host), you will not lose your work. Give it a try, for example, run the top command. You will get an output like the one below:

Notice how at the top of the terminal it says “screen”. You can detach this screen, meaning you leave the session running on the computer while using the terminal for something else. The command to detach it is “Ctrl+a d. You can also just close the window, to simulate losing connection.

4. Open a new terminal and run the command screen -ls this will list all the screens running (detached or not), for example

5. Re-attach the session. If there is only one screen, re-attach it by running screen -r, and you will return to the session you detached or closed. In this example, you will recover the screen runningtop. (Note that we never quit the top command.)

If you have multiple screens running, the output will look like this:

You can re-attach the session by its name, in the screenshot above, you could try screen -r 38966, screen -r ttys004.Angels-MacBook-Pro, or screen -r 38966.ttys004.Angels-MacBook-Pro, the result will be the same.

6. If you are going to have multiple screens, it is convenient to name each screen so you can quickly tell the difference between them, notice how the last screen is named amazoncluster, this gives you an idea what it is for. To name a screen, use the command screen -S screen_name when you start the screen, or check the manual. This will help you navigate between all your screens.

That is it for this post, but keep in mind there are many useful things you can do with the screen command, for example:

1- you can have multiple windows within a screen and easily navigate between them,

2- you can split your screen vertically or horizontally to show multiple windows at once,

3- you can log your session so you have a record of everything you type

4- you can lock a screen and add a password to it.

As with all Linux commands browse the manual, man screen to see everything you can do. I am sure you will find something useful.

On your marks, get set, code!

--

--

Angel Manzur

Physicist/data scientist with +10 years experience in academia/industry. Passionate of technology, computers and learning new tricks!