A short tutorial on screen command
By: Shubhi Asthana
Tags: Linux Commands, Terminal Commands, Women In Tech, Coding Shortcuts

Photo by Pixabay from Pexels

I came across the screen command earlier this year, when I was searching for a way to run my program persistently, even if my VM gets disconnected. I am not suggesting that you absolutely need it, but I strongly suggest learning it as you may require it at some point in your coding journey.

With all that said, let us talk in detail about what screen really is!

Screen is a very useful command that can be used to multiplex a physical console between several processes (typically interactive shells). It offers a user to open several separate session instances inside a one single terminal. When a session instance is detached or there is a network disruption, the process in that session instance will continue running, and wont be interrupted.

Hence the screen application is very useful, if you are dealing with multiple programs from a command line interface and for separating programs from the terminal shell. It also allows you to share your sessions with other users and run long processes persistently.

With that said, let us head on to the different commands for screen.

1. Installing the screen

Typically, the screen comes preinstalled in popular distributions. However I did have to install the screen once on a CentOS. The command to install screen is

pip install screen

or

apt-get -y install screen

2. Start a screen

You can start screen by typing ‘screen’ at the command prompt and a new screen session will be started which looks the same as the command prompt

screen

Since screen is an application, you can type ‘Ctrl-A’ and ‘?’ to see all commands or parameters on screen.

It is a good practice to start screen sessions with descriptive names so you can easily remember which process is running in the session. To create a new session with a session name, run the following command

screen -S name

Replace ‘name’ with a meaningful name for your session.

You can view all the current screens in your VM by doing ls

screen -ls

3. Detach from the screen

As I mentioned earlier, one of the advantages of screen is that you can detach it. Lets take a simple scenario:

Say you are running a machine learning model which would take approximately 2 hours to run. If you disconnect from the session, or suddenly the connection is lost by accident, then the download process will stop. You would have to start from the beginning again. To avoid that, we can use screen and detach it.

To detach from the current screen session you can press ‘Ctrl-A’ and ‘Ctrl-D’. All screen sessions will still be active and you can re-attach to them at any time later.

4. Reattach to the screen

To re-attach/restore to a session which you had detached from, simply run the following command

screen -r name

Here replace ‘name’ with the session name that you had earlier used. If you are unsure of the name, use ‘ls’ command to find list of active screens

5. Lock screen

You can also lock the screen. For this, press ‘Ctrl-A’ and ‘x’ shortcut to lock the screen. This is handy if you want to lock your screen quickly.

To unlock the screen, you would use your VM/Linux pwd

6. Terminate the screen

To terminate the screen session, you can either press ‘Ctrl-D’ or use the ‘exit’ line command.

Final Thoughts!

This is a summary of daily screen commands you would need. For all useful features of the screen command, you can check the screen’s man page

man screen

Questions or feedback? I’d love to hear from you — please feel free to leave out a comment, or connect with me on Twitter/Linkedin.