shell-screen

SSH: Basic usage of Shell Screen

If you are often running scripts in SSH, especially ones that have a long execution time, you might have experienced termination of your script before it actually finishes its task(s). Maybe you closed the terminal window by accident, or you lost Internet connection for a moment.

What you can do is run your script persistently with the help of Shell Screen.

Screen, as defined in GNU.org, is a “full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.”

shell-screen

How to use Shell Screen

Run Screen without a name:
Type “screen” right before running your script via SSH.

Run Screen with a set name:
You can set the name of your Shell Screen session by using this command instead: screen -S foo, with “foo” as your chosen screen name.

How to get the “name” of your Shell Screen session

Type “screen -r” on your terminal.

Then you will see something like…

[myuser@myserver~]# screen -r
There are several suitable screens on:
23390.pts-2.myserver (Detached)
10219.pts-2.myserver (Detached)
6051.pts-1.myserver (Detached)
27098.pts-5.myserver (Detached)
5834.pts-1.myserver (Detached)
15136.pts-13.myserver (Attached)
18953.pts-5.myserver (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

I have one window currently running a script via SSH, so I know that the attached session “15136.pts-13.myserver (Attached)” is the one I need to take note of.

If you set a name for your Shell Screen, then it might look something like 15136.foo (Attached), “foo” being the name you set.

Anatomy of Shell Screen session “name”

Now let’s talk about what the session name is made of, taking this as an example name:

15136.pts-13.myserver

15136 is the PID (Process ID?), pts-13 is the “tty” (file that represents a terminal), and myserver is the host name.

How to recover / resume a Screen session

Remember to first take note of the Shell Screen session that you’d like to resume. Then follow this syntax: screen -r
<INSERT_FULL_SCREEN_NAME_HERE>
. For example, screen -r
15136.pts-13.myserver
. Or if you named your Shell Screen session, you might be able to resume it like this: screen -r foo, where “foo” is the name of your Shell Screen.

How to kill a Shell Screen session

To quit a Shell Screen session, use the following syntax: screen -S <INSERT_PID_HERE> -X quit

For example: screen -S 15136 -X quit

Catzie

A Filipino who is a mobile & web developer on weekdays, and a baker on weekends. When I have time to spare, I blog, play video games, watch series (usually anime), cook dishes, draw, or spend a little time with pets. When I have the rare luxury of time, I get to travel and record song covers too! Follow on Twitter: @catzie

Leave a Reply

Your email address will not be published. Required fields are marked *