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.”
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
screen -r” on your terminal.
Then you will see something like…
[myuser@myserver~]# screen -r
There are several suitable screens on:
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 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. For example,
screen -r. 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
screen -S 15136 -X quit