You've used SSH to log into a shell account on a remote Linux or UNIX server and are happily waiting for a large download or a long compile to complete. All of a sudden, the connection between you and the server is dropped. You log back in, but what you were doing has stopped and you'll have to start again. How annoying!
Recently I have started using
screen. This allows you to start additional terminals that you can attach to and detach from. Being able to switch between them means that you can be running a large compile or download and still be able to get on with other things. That's nice, but what is really cool is that these terminals will last between your SSH sessions.
This means that you can start a screen terminal, do some work in it, detach from it, log out, go to another computer, log in again, re-attach to the terminal and it's as if nothing ever happened. If your connection is dropped, then the screen terminal will still be there too - just reconnect, re-attach and keep going.
So, this must be really hard, right? Wrong!
Check Screen Is Installed
First, we'd best make sure the screen program is installed. Once you have SSH'd into the box that you are interested in being able to use screen on, issue the command
screen -v
The result of doing this should look something like this:
jnthn@jnthn:~$ screen -v
Screen version 4.00.03 (FAU) 23-Oct-06
If not, then you need to install screen. How to do this will vary depending on what package manager, operating system and/or distribution you are using. Consult your usual source of packages. Alternatively, you can build and install screen from the
source.
Starting A New Screen Terminal
Starting a new screen terminal is as straightforward as typing:
screen
The result of this will be a copyright notice with a prompt to press enter or space. Do either of these, and you will be presented with a normal shell prompt. You are now running inside a screen terminal. To demonstrate this, we will run some long-running command, then detach from the terminal, disconnect, reconnect and reattach.
Running Commands
You can run things inside of screen just as you normally would. For example, we can run this one-line Perl insult script, which will insult you every ten seconds. Leave it for a little while to print the first message.
jnthn@jnthn:~$ perl -e 'while(sleep 10){print reverse split //, "\n!llems uoY"}'
You smell!
You smell!
Detaching From A Terminal
Now we want to detach from this terminal. Hold down the "Control" key on your keyboard and press "a" once. Now release ALT and press "d". You will find yourself back at the terminal where you started screen.
jnthn@jnthn:~$ screen
[detached]
jnthn@jnthn:~$
Notice that you were able to detach while a program was still running. Our insult program is in fact still running in the background.
Reattaching To A Terminal
We want to reattach to the terminal to see how our insult program is doing. This is achieved using the "-r" flag, along with specifying the terminal that we want to reattach to. The thing is, how do we find out what it is called? The answer lies in the "-list" flag, which provides a list of all terminals.
jnthn@jnthn:~$ screen -list
There are screens on:
9547.pts-39.jnthn (Detached)
1 Sockets in /var/run/screen/S-jnthn.
We can now take the name of the terminal and reattach to it.
jnthn@jnthn:~$ screen -r 9547.pts-39.jnthn
Where we will see that we have been insulted plenty. Do Ctrl+a,d again to detach.
Surviving A Disconnect
Back in the original terminal, type exit to end the SSH session.
jnthn@jnthn:~$ exit
This is a controlled disconnect, where you detached from the screen terminal first. If you reconnect now, you will find that your terminal is still in this list.
jnthn@jnthn:~$ screen -list
There are screens on:
9547.pts-39.jnthn (Detached)
1 Sockets in /var/run/screen/S-jnthn.
Then you can reattach as before. It may happen that your disconnect is not controlled though - you lose the connection while inside your screen terminal. If this happens, the terminal may not be detached properly. In this case, when you reconnect you should also supply the "-d" flag. This will detach the terminal if it is currently attached "elsewhere". This is also useful if you forgot to detach when logged in from a different computer, and if you are still logged in there.
jnthn@jnthn:~$ screen -r -d 9547.pts-39.jnthn
You can shorten that to:
jnthn@jnthn:~$ screen -rd 9547.pts-39.jnthn
Ending A Screen Terminal
If you are finished with a screen terminal, simply attach to it and use the usual exit command.
jnthn@jnthn:~$ exit
Back in the terminal where you started screen, you will see the message:
[screen is terminating]
Being on IRC, 24/7
While screen is useful for all of the above things, for me it is most useful when combined with an IRC client. I work on a medium-sized open source software project and there is an IRC channel where a lot of the developers hang out. Using
irssi (a text-based IRC client) and screen, I stay connected to the channel 24/7 and don't miss any messages for me. I can also move between computers easily (a bonus since I may be working in the office, back home or on my laptop if I'm on the road).
Conclusion
If you work on remote servers regularly, screen is probably a useful tool to know about, whether you're doing compiles or just routine system administration where you'd rather not have a disconnect get in the way of your work too much.