an introduction to screen - AstrobioMike/JPL-HBCU-2020 GitHub Wiki
The screen
program let's us start a terminal session that will be safe and keep running even if we get disconnected from our instances, or if we just want to disconnect but leave the process running because maybe it takes a long time. This is going to be a very quick intro to a few fundamentals of screen
that will let us utilize this very helpful program 🙂
- Connecting to our instance
- Starting a
screen
session - Detaching from an active
screen
- Re-attaching to an active
screen
- Exiting and closing a
screen
- Starting a
screen
with a specific name - A note on re-attaching to a
screen
that says it is currently attached - Summary
If we don't have an instance running right now (check in your JetStream account), and we want to actively follow along with this page, we should start one up following the steps laid out here.
Then once we have it running, we want to connect to it. There are 2 paths for doing that laid out here.
Now that we are in our command-line environment, the easiest way we can start a screen
session is by just entering the command screen
, e.g.:
When we execute that command, our terminal environment changes, giving us some information about the screen
we are using:
When we press enter
, we are returned to what looks like a typical prompt like we just had, only now we are in a screen
:
If we want to double-check that we are, we can use screen -ls
to list the current screen
's that are running and if any of them are currently "attached", e.g.:
screen -ls
That "5099.pts-0.js-169-239" business is the screen
name that was generated when we just executed the screen
command by itself above.
Now we can do things as usual in this session, and if we get disconnected, or we just want to disconnect, any process we have running here won't care and will continue on its merry way.
To show an example of this, I'm going to just start a slow counting process on the web-browser connection I am in that will print out a number every 10 seconds (if you are new to Unix for-loops and want to learn me, see here 🙂 ):
for number in $(seq 1 1000)
do
sleep 10
echo "$number"
done
Now that that's running, let's look at how to detach from that screen
while it's doing work.
To detach from a screen
and leave it running, we need to use some keyboard action. In the screen, we want to press ctrl + a
then release both of those (this puts us in a sort of alternate keyboard mode), then we want to press the d
key. That will return us to our original terminal session, and tell us we have detached from that screen
, e.g.:
And now if we run screen -ls
, we will see that screen is still running, but now it says detached instead of attached:
screen -ls
Great. I'm going to close my browser now, and then reconnect to my instance through my terminal rather than the web-browser (as detailed here).
And if we run screen -ls
in this new connection, we can see there is still the other screen
session running:
screen -ls
Now let's look at how to re-attach to that currently running screen
.
To re-attach, we just need to provide the -R
flag and name of the screen
we want to re-attach to. We can see the name of the screen in the output from screen -ls
and copy it, then I would re-attach to the screen
in this example like so:
screen -R 5099.pts-0.js-169-239
Which switches my terminal to being in that screen
session that is listing out numbers every 10 seconds:
We can cancel that process now, by pressing ctrl + c
:
Which gives us our regular prompt back. And we can check we are still within the screen
with screen -ls
:
Now let's look at how to exit a screen and close it for good (rather than detaching like discussed above).
Exiting a screen will shutdown that screen and it will be gone forever, so we only want to do this when we are done with it. But to close a screen
for good, we just need to type the command exit
:
And when we hit enter
, it will bring us back to our regular terminal, and tell us the screen
in terminating:
And if we run screen -ls
now, we will see there are no active ones anymore:
If we want, we can name the screen
session when we start it by providing the -R
flag followed by a name just like when we re-attached above. If the name we provide doesn't exist yet, it will be created, e.g.:
screen -R mikes-screen
That brings us into a new screen
, and now if we run screen -ls
:
There are still some numbers in front, but we can more easily recognize which screen has which process if we want to start more than one and name them as something useful to us.
Typing exit
to exit that new screen
and bring us back to our regular terminal:
Sometimes if we get kicked offline or something else weird happens, we may find ourselves in a situation where the screen
is still attached to something, and it won't let us re-attach to it. To get around this, we just need to add one more flag (-d
) to the command we are using to try to attach, which will tell the screen
to first detach from anything, and then let us attach.
We don't have this situation setup for us, but it would look like this:
screen -dR mikes-screen
Which right now just starts a new screen
, so we can exit with exit
again if you ran that.
And that's all we really need to know to start using screen
to our advantage. It is extremely useful when working on remote computers, and it will likely be available in most or all of the environments we work in.
Start a new screen
with the name "assembly":
screen -R assembly
Detach from a screen
and leaving it running:
ctrl + a
release, then press d
List the currently active screen
s and see if they are attached or not:
screen -ls
Exit and close a screen
(while attached to it):
exit