Screen is a terminal multiplexer which helps you to start a screen session and open any number of windows inside the session.
Sometimes, when we perform a long-running task on a remote machine and suddenly the connection lost or your client machine have problem, the SSH session will be terminated and all of your work is GONE.
It happend many times especially in training a Machine Learning/ Deep Learning models because sessions are dependent on your client machine. Fortunately, we can eliminate the problem by using a utility called screen
sudo apt-get install screen
screen
Now, you can run a long-running task like training a Deep Learning models, download very heavy files without worrying about the connection lost .
To detach an attached screen :
screen -d
Press CTR-A then press d to detach but preserve the screen, you can re-attach later.
screen -r
This command will re-attach to your current running session if you just have 1 screen session.
In case of you have more than 1 screen session, you can use the command below to see all sessions:
screen -ls
The results will be like:
1(base) medic01@u20:/data1/kynd$ screen -ls2There is a screen on:3 3350892.pts-9.u20 (2022년 03월 28일 20시 42분 19초) (Attached)41 Socket in /run/screen/S-medic01.
Now you can get the screen id and re-attach by that id by typing:
screen -r your_screen_id
screen -S session_name # Note: use a capital S
You will see your session with name like below:
1(base) medic01@u20:/data1/kynd$ screen -ls2There are screens on:3 3351224.session_name (2022년 03월 28일 20시 46분 01초) (Attached)4 3350892.pts-9.u20 (2022년 03월 28일 20시 42분 19초) (Attached)52 Sockets in /run/screen/S-medic01.
Re-attach to your screen session by:
screen -r session_name
To exit from screen, simply type exit
in your screen session. It will terminate your current screen session and back to normal ssh session