Linux: resume your lost shell sessions using the “screen” command!

Wouldn't it be nice if there was a tool to resume a lost ssh connection? Wouldn’t it be great if you could go back in time and not start a…

Linux: resume your lost shell sessions using the “screen” command!
Photo by Diego González on Unsplash

Linux: resume your shell sessions using the “screen” command!

Wouldn't it be nice if there was a tool to resume a lost ssh connection? Wouldn’t it be great if you could go back in time and not start a long-running process in this Linux server 10 minutes before your shift ends? let’s be realistic you can’t go back in time; most likely, if time travel had been invented, we would already know it by a time traveler, but don’t lose focus! this can be the content of another article; the most realistic solution would be to use the screen command!

What screen does

From the man pages of screen we read in the description:

“When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between windows, view the scrollback history, switch between windows in whatever manner you wish, etc. All windows run their programs completely independently of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal. When a program terminates, screen (per default) kills the window that contained it. If this window was in the foreground, the display switches to the previous window; if none are left, screen exits.”

In simple terms screen allows us to resume a local or remote session of a shell and its running programs even in case the session is user detached or even lost because of a network error.

How to install screen

If screen is not installed on the machine you want to manage its sessions, the OS package manager can easily install it.

Install screen in Ubuntu / Debian

$ sudo apt install screen

Install screen in Centos / RedHat

$ sudo yum install screen

How to use screen

After login to the local or remote host enter

$ screen

You can detach the session by entering CTRL+a d

$ screen 
[detached from 35.pts-0.nautilus] 
$

This will create a no named session which is not a good idea in case you plan to create multiple sessions, the best is to use the -S parameter to create a named session.

$ screen -S session1 
[detached from 44.session1]

Using the -ls parameter, we can get a list of our user-detached sessions

$ screen -S session1 
[detached from 44.session1] 
$ screen -ls 
There are screens on: 
        44.session1     (01/13/23 20:53:45)     (Detached) 
        35.pts-0.nautilus       (01/13/23 20:52:49)     (Detached)

Using the -r parameter, you can restore the session you wish; in this example, we are restoring session 44; if session 44 had left with a program running in the background or the foreground before detaching, the program will still run unless it has completed its execution.

screen -ls 
There are screens on: 
        54.session1     (01/13/23 20:57:13)     (Detached) 
        44.session1     (01/13/23 20:53:44)     (Detached) 
        35.pts-0.nautilus       (01/13/23 20:52:48)     (Detached) 
3 Sockets in /run/screen/S-root. 
$ screen -r 44

To exit the session, you can detach and run.

$ screen -S 44.session1 -X quit

But what about killing all sessions quickly? you can use the following trick

$ screen -ls |  grep -i detached | awk '{ print $1 }' | xargs -I {} screen -S {} -X quit 
$ screen -ls 
No Sockets found in /run/screen/S-root.

Conclusion

screen is a powerful tool that every Linux user must know! its a life saver and very easy to learn just the basics; who knows, it might be that time travel didn’t ever invent because there were not too many angry System admins because they knew about the screen command :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…