Linux Screen Command – How to Multitask in a single SSH Session

I will start this post by stating something that happened to me. This was what actually inspired this inspired this blog post.

On that day, I was on a amazon EC2 server. I was setting up CUDA, Pytorch for a image processing project.

Then, i had this scraper i just built. I needed to run it on the server. A single execution would run for around 4 hours.

Yes, i know what you’re thinking. Why can’t i just open a new terminal and fire up my scraper there.

Now, Lets assume i opened another SSH session in a new terminal for the scraper.

But then i had another twitter bot i wanted running – Another side project.

I will assume i also opened a new SSH session in another terminal window for the twitter bot.

But i can’t keep opening a new SSH session for every task i needed to run. What if i had around 20 different scraper to run.

Do i open a new SSH session for every task ?

Okay Let’s assume i have this crazy super 8th generation computer and i could open a new SSH for every damn task i needed to run at the same time.

So,

Another question for you – What happens if your session was terminated due to break in connection ?

Like you had this 4 hours installation going on which requires you to enter a password at the end of the installation for finalization.

Then you were at the point of finishing and entering the sudo password, you had a glitch in your connection and the connection was lost.

What happens ?

Do you restart the installation all over ?

The solution is – You need a way to resume back your session. And this is what this post is about.

The title of the post might as well be – How to resume your SSH session.

but it’s more than that.

Okay enough, i’m only giving you enough reason why you might need to multitask in a single session.

Then, how about i tell you that:

There’s a way to run all those 20 scrapers and bots in a single SSH session at the same time. And, even after a lost in connection you can resume them all or whichever you want to. All in a single session.

Linux Terminal Screens – Many Screen in a single session.

What is a Linux Screen Session

A linux screen session is a session of an utitlity called screen. Screen is a linux tool that helps you manage multiple console screen display in a single terminal session.

Firstly, Let’s check if you already have screen installed. If not, we install the screen utility for linux.

root@geekykpage:~# screen
screen: command not found

Okay, I don’t have it so i’m going to install it now.

root@geekykpage:~# sudo apt-get install screen 
Reading package lists... Done
<...>

After installing screen, you should type screen again.

root@geekykpage:~#screen 

GNU Screen version 4.06.02 (GNU) 23-Oct-17

Copyright (c) 2015-2017 Juergen Weigert, Alexander Naumov, Amadeusz Slawinski
Copyright (c) 2010-2014 Juergen Weigert, Sadrul Habib Chowdhury
Copyright (c) 2008-2009 Juergen Weigert, Michael Schroeder, Micah Cowan,
Sadrul Habib Chowdhury
Copyright (c) 1993-2007 Juergen Weigert, Michael Schroeder

From this Introduction screen, you can move on to the next screen, detach the screen or just kill if off.

But:

Don’t mind this, We shouldn’t set any room for confusion. So, just hit CTRL + A then press D to detach from the introduction screen.

And you should be returned back to your normal terminal screen.

root@geekykpage:~#

How To Create A Screen Session

Now, for demonstration purpose, i will create a new display screen for the sole purpose of running top command which helps me monitoring my computer usage.

root@geekykpage:~#screen -S monitoring

The -S parameter option is for specifying the name of your new screen session.

After the execution, you will be presented with a new clear screen. This means your new screen has been created.

Now, we will run the top command.

root@geekykpage:~#top
top - 09:08:23 up 45 days,  7:29,  5 users,  load average: 0.05, 0.01, 0.00
Tasks: 154 total,   1 running, 115 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1009172 total,   118120 free,   713700 used,   177352 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   115456 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
16575 root      20   0   44552   3856   3196 R  0.3  0.4   0:00.05 top
    1 root      20   0   77944   4448   2024 S  0.0  0.4   3:02.54 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.14 kthread
    4 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 kwork..
    6 root       0 -20       0      0      0 I  0.0  0.0   0:00.00 mm_pe..

While this is running, you now need to edit a script in the same SSH session. It’s simple. You detach from the current display and create a new display.

How To Detach From A Screen Session

To detach from our “monitoring” screen, use CTRL + A, then type D.

This will detach you from the screen session, and you will be returned back to the original console terminal session.

[detached from 16677.monitoring]
root@geekypage:~#

We can now create our new screen for editing your script. you can name that session “editor“.

You can now edit your file without affecting you monitoring screen,

you can always switch back to whichever screen you want to work on. Read on

How To List Your Screen Sessions

Now, we have two screens sessions. How do we list them . it’s simple with the -ls parameter

root@geekykpage:~#screen -ls
There are screens on:
        16677.monitoring        (12/29/19 09:14:38)     (Detached)
        13561.editing  (12/29/19 09:03:24)     (Detached)
2 Sockets in /run/screen/S-root.

There you have it. So how do i resume a session, say the monitoring screen.

How To Resume A Screen Session

root@geekykpage:~#screen -ls
There are screens on:
        16677.monitoring        (12/29/19 09:14:38)     (Detached)
        13561.editing  (12/29/19 09:03:24)     (Detached)
2 Sockets in /run/screen/S-root.

I now need to resume my monitoring session.

And we are back to our first screen. The monitoring screen.

How To Terminate or Kill a Screen Session

Meanwhile, I’m now done editing the python script. I now need to kill that session so we are only left with monitoring.

If you are in the current session you want to terminate.

CTRL+A then type “K

But:

If you are already detached from the session. You need to do something like below.

root@geekykpage:~#screen -X -r monitoring kill
root@geekykpage:~#

Where “monitoring” is the name of the session.

Linux Screen Alternatives

Lastly, For one reason or the other, you might not want to use the screen command. These post detail 3 different alternatives to the Linux screen command.

READ: …

If you have any question or contribution to make this article, please you feel free to drop a comment below.

I will be more than happy to reply you ASAP.

Leave a Reply

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh