Bismillaahir Rahmaanir Raheem
Note: This is a post I started writing back in November, but only got around to updating now.
Alhamdulillaah, I cannot believe I’ve lived for so long without knowing about screen (Wikipedia article). To put it as simple as possible, think of screen as a “tabbed” interface for terminals/shells. This is a weak analogy, however, because it enables so much more than tabs do for browsers, for example. Onwards for more details.
I had first heard of screen through references and then through some of my friends, but I didn’t give it much heed because it seemed like something too complex for something so simple as a terminal application. Little did I realize that, like most applications with their roots in Unix, it’s both simple and extremel useful.
screen is invoked by typing it’s name. It will appear initially as though you’ve reset your terminal, but this is just a new screen session, which is effectively a new login. Each screen window (which I will explain later) is independent and has it’s own scrolling buffer, independent also of your own terminal’s buffer. This means that screen as an application stores previously displayed text on the machine on which it is ran, so if you’re running it remotely, it won’t affect your local machine.
Commands in screen are almost always prefixed by ctrl-a. For example, creating a new window in screen is “ctrl-a c”. Switching to the next window, if you have more than one, is “ctrl-a n”. Previous is “ctrl-a p”. screen windows can be exited by simply typing “exit” as if they were any other terminal session. When the last screen window is exited, screen itself terminates. Screen windows can also be killed by typing “ctrl-a K” (note the capitalization of “K”).
It’s also easy to jump to windows 0 – 9 by typing “ctrl-a” followed by the window number. So, for example, if you want to jump to window 3 of your screen session, just type “ctrl-a 3”.
Detach and resume
The real beauty is using screen is its persistence. Chances are, if you’ve used remote login shells frequently, you’ve been disconnected while working. Most shells will respond by terminating whatever was actively running in the foreground of that terminal. This means your vim session was interrupted. It also means whatever service you had started is probably dead by now. screen takes care of all of these things, because it behaves as a persistent application. Thus, whenever you disconnect from the terminal, screen stays running, and it allows you to resume a previous screen session with the command line option “-R”, which means reattach an existing screen session, else start a new one. Additionally, the “-d” argument might be useful, because it will detach the session from an existing login, in the case it is still attached elsewhere, such as a remote login from another location that you’re no longer using.
For example, if you are logged-in to your server from home with an active screen session, and then go to work, calling “screen -d -R” will detach the screen session from your home login and reattach it, where you left off, to your login from work.
Another advantage is that screen effectively allows you to make a daemon (background running process) from any command-line application, including those for which it normally isn’t possible. An example of this would be rtorrent, which is a torrent downloading application normally meant to run in the foreground. I personally like to start an rtorrent process running in one screen window and just leave it running. Then whenever I login to that server and attach the screen session, I can check on the status of my torrents very easily by ctrl-a p/n to the appropriate window while doing other work.
So there you have it! I realize this is a very introductory article with regards to screen, but it should be enough to get your started. If you run into any problems with it, just type “ctrl-a ?” (that’s a question mark) and a friendly window will pop-up explaining, albeit briefly, all the different commands and what they do.