Tardis Beginner Tutorials/4
Screen is a very useful and powerful program that lets you create and manage multiple virtual sessions independently of how or where you're logged into a machine. If you don't understand what i mean, don't worry - it's simplest to explain it by example. If you create a screen session, it gives you a command prompt again - think of it as a shell within a program. That's pretty much all there is to it - you can run anything as normal in this new shell. The advantage is, you can disconnect your current login and the shell will not terminate - whatever was running in it will still be running, whereas normally if you log out all your child processes are killed.
Creating a screen session and running a program in it
Now we will create our screen session. Simply type screen and you will get a splash screen at which you press space, then your terminal will clear. While in screen your terminal will not beep as before, but instead will flash visually when it receives a beep command from an application. Try pressing backspace a few times on an empty prompt to see this in action.
So now let's run our program - something with constant visual output. Type top and you will get a nice list display of the top (!) processor intensive processes running on the machine you are logged in on and various information about them, along with general system stats. These are often interesting to watch :) Now simply close your putty window / kill your ssh connection without logging out. Then log back in, as described in tutorial 1. Now you want to get your screen session back. Just type screen -r to get it back (-r for retreive)... couldn't be simpler! You now have your top window back. Press q to exit from top and type exit or logout to close the screen session and return to your normal prompt.
Running multiple screen sessions and different ways of attaching
One useful feature is our ability to name a screen session to be able to identify it later. Create a new screen session and run top in it again, but this time we name it using -S (case is important):
screen -S mytopsesh top
Now we can detach the active screen session and leave it to run in the background by pressing ^a-d (that means ctrl-a followed by d). To tell screen you want to send it a command, rather than sending it to any program you have running, you press ^a to let it know you're talking to it, then follow it with one of the command codes it recognises. For more information read man screen. So now you've detached your first screen session, let's create a second one to run something different in - say an editor session you want to leave running.
screen -S myothersesh nano
Then detach it again with ^a-d as before. Now if you type screen -r you will get a list of possible screen sessions to reattach, with their number and name - you can recall them by name using screen -r mytopsesh or just by number, which you have to do if you didn't name them when you created them. So now you have multiple interactive sessions running accessable from one terminal window, which you can close without them terminating. Now open a second putty connection alongside the first, as described in tutorial 1 again. From here we can reattach any of the screen sessions we created on the other connection. Type screen -r myothersesh and you will see the editor again. Now type screen -rd myothersesh on your first login session and the editor screen detaches from where you just attached it and comes back here again (the -d detaches if necessary). And to finish off here's something cool to try: Type screen -rx myothersesh on the second login box (the one where screen isn't attached currently). This reattaches the screen session as usual, but WITHOUT detaching it in the first location. Now try typing someting in the editor in one of the putty sessions, and then the other. Isn't screen cool?