Predictive Hacks

Basic Usage of Linux Screen

screen

Screen lets you run terminal applications to the background having the option to call them back whenever you want. Its real power is that you can use it over SSH connections even if you disconnect and reconnect.

But how this is helpful for us?
The screen command is a great way to deploy APIs. Also, we may want to train a heavy deep learning model on a server but we don’t want to keep the connection alive(we may want to turn off our pc).

Installation

You can install screen on ubuntu running the following:

sudo apt-get install screen

Screen Basics

To open a new screen session you run the command below.

screen

or if you want to give your session a specific name run:

screen -S session_name

When you do this it will create a new session with a cell in it. Now you can run whatever you want as usual. To detach this session you should press the following.

control+a d

Now, If you are using an SSH connection with a server for example, after detaching, you can close your terminal window and your script inside the screen session will stay alive. You can easily access the session if you reconnect with the server and attach the session as we said above.


Run the following to get the list of screen sessions:

screen -ls

It will show you your sessions like follows

(base) $ screen -ls
There are screens on:
        26489.neural_network_training      (Detached)
        15340.predictive_hacks_api (Detached)
        15319.streamlit_app  (Detached)

To attach one of the detached sessions:

screen -r session_name

or 

screen -r session_number

Then, type the following if you want to exit the session.

exit

If you attached a session that is running a script and you can’t type, to terminate it, press the following.

control+c

Bonus

How to start a new session, give it a name and run a script with a single command:

screen -S session_name your_command_to_run_the_script

How to start a new session, give it a name, run a script and detach it with a single command:

screen -dmS session_name your_command_to_run_the_script

How to detach a session that says it’s already attached and you can’t access it:

screen -r -d session_name

how to kill a detached session

screen -X -S session_name quit

How to kill all screen sessions

killall screen

Examples

Let’s say we want to deploy a Flask API using screen.

screen -S flask_api_name python app.py

Then, if everything run without any problems we can detach the session using control+a, d.

How to deploy a streamlit app in detached mode:

screen -dmS streamlit_app_name streamlit run app.py

Summing It Up

In this post, we saw some basic stuff of the screen command that can help you use it for your APIs, python scripts, etc. Head to the screen user’s manual if you want to learn more.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s