Thursday, April 8, 2010

Detaching from terminals using GNU screen

GNU screen is a Linux utility, that I did not know much about it, until a couple of years ago. It is a very handy program, and I'll start with my most frequent use of the utility.

If you use Linux, you like terminals. Let's say you start a program like Firefox or emacs from the terminal, by issuing a command like

$ emacs &

The program runs in the background and let you do other work in the terminal. However, when you exit the terminal, by either typing exit or closing the terminal window, your program (emacs in this case) is also killed.

GNU screen allows you to detach the program from the particular terminal from which it was invoked, thereby letting the program persist beyond the life-time of the terminal.

It is very simple to use. Fire your program from a terminal, in background mode.

$ emacs &

Then invoke screen (preinstalled on pretty much all Unix like systems)

$ screen

You may see that the title of your terminal window has changed somewhat, but that is not important.

You now type Ctrl-A d. You should see something like [detached] echoed on your screen.

Now go ahead, and kill your terminal.

Your emacs window persists!

Magic!

There are several other good uses for screen. You could fire programs like Thunderbird, Firefox, emacs etc. from a terminal and leave them running for months. If you've logged into a remote server, screen can function as a multiplexer (multiple tabs).

There are a number of places to learn more about screen. Hereare some of them.