Take advantage of gnu-screen

Written by
Date: 2012-12-01 21:13:33 00:00

A lot have been written about gnu-screen, but I think I can still write a little about it.

It is a well know tool among old Linux guys, but it is almost unknown among newcomers.

What is gnu-screen, and what it does for you?

gnu-screen, is a tool from the gnu project, that is available in every Linux distribution I have used, and I'm sure it is available in all of them, and you can install it on your Mac too. It is a virtual screen, or better said, a virtual terminal, you can attach and detach every time you need.

You start a new screen session, run some command in there, detach the terminal, come back in minutes or days (provided that the computer have not been restarted), and when re-attach the screen again, you will find your job exactly where you left it.

OK, now we have an idea about what gnu-screen does, but, when should I use it?

I use it specially when working with remote servers, where I connect via ssh. Here what a typical application of screen could be.
You start a screen session:

screen

Start compiling some program, one really big that is going to take a lot of time, and detach the screen. Just got to another terminal, and run:

screen -ls

You will see something like:

11904.pts-1.li367-250   (12/01/2012 06:58:06 PM)        (Detached)
    11655.pts-1.li367-250   (12/01/2012 06:52:48 PM)        (Attached)

This part is the "screen ID", in this case: 11904.pts-1.li367-250. Take that, and type:

screen -d 11904.pts-1.li367-250

You can also detach a screen using your keyboard, press CTRL+a and then d

You can now close your ssh session, and even power off your local computer (or phone, as these days you can use it too).

Later, just ssh to the server again, and attach the screen again:

screen -r 11904.pts-1.li367-250

That is it. Simple and really useful.

Bonus: If you are working on a screen, and suddenly your connection goes off, your job is not lost, just wait until you can reconnect again, and re-attach the screen, and you will be where you left.