Create a Screen Session on a Remote Server

You can execute a code on a remote server, disconnect from the server, and then come back and see the result.

Create a Screen Session on a Remote Server

First open a Linux terminal and connect to the remote server using

ssh <USERNAME>@<SERVER_NAME>.<DOMAIN>

Then, check if screen installed.

screen --version

You can see output like below

Screen version 4.08.00 (GNU) 05-Feb-20

Now you can create a screen session by following command

screen -S <NAME>

The -S option helps you to choose a name for each session. It is useful for later, when you have more than one session. Then, navigate to your target directory and activate your environment if you are using Anaconda. Inside the session, type your execution command. For example

(myenv) user@server:/project_directory$ python main.py

After your code started to run successfully, you can detach from the session by pressing Ctrl + a and then, ctrl + d. You should something similar as output.

[detached from <SESSION_ID>.<NAME>]

Now, you can safely close your terminal. Later, you can reattach by the following command:

screen -r <NAME>

If you forgot your session name, you can see all active sessions by:

screen -ls

And, finally, when you want to completely terminate the session you can simply press Ctrl + d from inside the session. The following message will appear.

[screen is terminating]
Mohammad Ali Zamani
Mohammad Ali Zamani
Senior Machine Learning Applied Scientist

I am a Senior Machine Learning Applied Scientist at Hamburg Informatics Technology Center (HITeC) and a Research Associate at University of Hamburg. My research interests include Deep Reinforcement Learning, Computer Vision, Cognitive Robotics and Natural Language Processing.