I find myself stuck in situations where I can’t really leave or close my shell session,

but I have to go somewhere else, disconnect my internet, or otherwise lose shell access for some reason or another and unable to get back where I left off.
Things like this happen sometimes, right? Or is it just me?

The solution for this is screen

Overview of Screen:

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows.

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 the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, switch between windows, etc. All windows run their programs completely independent 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 previously displayed window; if none are left, screenexits.
https://www.gnu.org/software/screen/manual/screen.html#Overview

 


To install screen:

Debian
sudo apt-get install screen
RHEL
yum install screen

That’s it!

To start your first screen:

screen -S [SCREEN NAME]

I’ll call my screen “example”:

screen -S example
  • starts a screen with the name of “example”

This is for all I care, a normal shell. Nothing is really different, and it shouldn’t be. Because screen adds flexibility  and convenience without changing what you are already using.

Proceed executing commands as you normally would in a shell.

 

Disconnect it to test:

 


Resume an existing screen

Oh no! I lost my session. My internet flapped or my computer crashed or Godzilla destroyed everything  EVERYTHING IS RuINED!

but I can resume my shell session now!

you can use:

screen -r [SCREEN NAME]

the next time you SSH in to your server, to pick up where you left off.

screen -r example
  • resumes a disconnected screen with the name of example

Success:


Sometimes even though you’ve disconnected, screen won’t let you back in.

Go ahead and disconnect that session

screen -d [SCREEN NAME]

 

screen -d example

Then try again to re-connect:

screen -r [SCREEN NAME]

screen -r example

 

 


0 Comments

Leave a Reply

Avatar placeholder