Once in a while I get into trouble because I want to run a programm which will take a longer time, and I have to control this program partly remotely. The main problem is here that all jobs in a shell are broken down when you close the shell.
So if you are going to compile a whole KDE session and want to do this remotely, you better make sure that the remote session is open, stable and secured all the time.
This situation is very annoying.
To get rid of this problem there is the program gnu screen. It comes with the ability of starting a terminal client daemon. If you start such a screen terminal within your standard terminal, in the first moment nothing changes – but there you are able to start other terminals as well as disconnecting from these terminals without closing them – you simply set it to a daemon mode where it continous running (to compile something for example).
Let’s give it a try!
(Note: all commands are case sensitive – so if I write Ctrl+a+S, I mean a capital S, while Ctrl+a+s means a small s!)
How to use
How to do? It’s quite easy. First you have to make sure that screen is installed – that is the case for the most standard linux distributions I gues, but in the worst case install it with the package manager of you choice (like yum, apt, urpmi, yast, emerge, …). After that you are ready to test it.
First open a normal terminal like konsole in KDE. Input screen and hit enter – nothing should have changed, but probably the titlte of the terminal window changed it’s name.
Now you can start a program which is running for an infinite time – ping http://www.google.de is a good candidate here. If you now want to close the terminal, you normally would kill the signal – but this time you can just hit Ctrl+a+d. You will then realize that you are back in the old terminal where you started screen, and there you will find the small but important information [detached]. This means, that you now detached screen from your terminal session, and that it will continue running as a daemon.
You can check that the program is still running in the background:
$ ps -A ux|grep ping
liquidat 18387 0.0 0.0 1792 724 pts/2 S+ 17:10 0:00 ping http://www.google.de
Imagine now that you are going somewhere else, and want to have a look at your process again. You open a ssh connection to your server, and now you have to attach screen to this terminal session – this is done with the command screen -r. There you will get the newest output of the still running screen session. You can now repeat the detaching of this session or you can kill the command, and close the session – whatever you want.
Two hints here:
First: Ctrl+a+D with a capital D instead of a small d does not only detach the screen session but also closes the parent shell.
Second: It is possible to start different screen session also, when you call screen instead of screen -r. But than you must choose between the different screen session when you hit screen -r.
Access by other people
How often have you thought about teaching-by-terminal? Like accessing a terminal which another user has opened to show him or her how to do stuff? Well, guess: it is no problem with screen.
Open a screen session on one computer. Access the computer with a different terminal, but from the same user, and hit screen -x. You are now in the same screen session, and can enter something, which is live displayed in the screen session of the other user. This is very, very useful if you have a teching login where every user can login and attach to your screen session – you can then show them what to do 🙂
But the ability to detach sessions, although it is already very practical and useful, is not the only important ability of screen: you can also start several terminal session in one screen session – since you can set the whole screen session in daemon mode you are able to continue twenty ping session while you are logged off for example. Ok, that doesn’t make to much sense, but if you have o run several time consuming processes in parallel, this is very interesting.
Here is what you have to do: open a terminal, call a screen session with screen like already explained, and start the ping again (to have a live example). Now hit Ctrl+a+c – you will get a new session. Start there another time consuming process like pinging another server. Hit Ctrl+a+c again, and you will get a third session – and so on and so on. To get back to the other sessions, there are different ways you can go. First you can walk through the processes with Ctrl+a+p and Ctrl+a+n (p for previous, n for next). Another option is to get a list of all open sessions, and to choose these with the arrow keys – hit Ctrl+a+” to see this list.
You can also toggle through the sessions with Ctrl+a+a or choose them with Ctrl+a+number, where number is the number of the session you like to access, like 1 or 2.
Btw.: if you now want to quit screen (quit, not detach), you can either go thorugh all different opened session, and quit them by hand, or you can hit Ctrl+a+:quit which forces all sessions to quit at once. This is particularly usefull if you have some ten sessions opened.
Copy and paste, scroll back
You probably got the idea now: screen can do a lot more than I can show you in this post. But one of the things I want to introduce here also is the ability to copy text: if you hit Ctrl+a+Esc the session output is freezed, and you can navigate with the arrow keys. If you reached the first char of the things you want to copy, hit enter or space. Navigate to the last char, and hit enter (or space) again – the marked text is then copied to the clipboard. If you want to paste it now somewhere, hit Ctrl+a+]. This is also useful if you just want to scrollback in a given output. Just hit to leave the copy mode without copying anything.
But what is if you want to keep an eye on several different sessions? Well, once again, screen is the tool of your choice: hit Ctrl+a+S (beware: capital S!). Now you have split the screen session into two areas, with the second area completely empty. Hit Ctrl+a+S again, and the screen is now split into three areas. To toggle through these areas, hit Ctrl+a+Tab. What to do now with these empty areas? Hit Ctrl+a+a or another navigation command as explained above to access an already running session. Or just hit Ctrl+a+c to open a new session.
If you want to resize parts of the areas, hit Ctrl+a+: and enter the resize command. For example, “resize 3” will resize the current area to the height of three lines.
If you want to lock your screen sessions away because you are going away to fetch a cup of coffee, just hit Ctrl+a+x – you need the user password to reaccess the terminal. But be careful: it is still possible to access running screen sessions with screen -x!
Screen comes along with even more functions – one thing is a tab bar which shows you where you are. There is also the ability to log to different files, and so on. To have a look at these options, check the different sources on the net. The best I’ve found in english is form the Gentoo wiki: TIP Using screen. Very useful and interesting – helped me a lot to write this tutorial (which I wrote more for me to remember and go thorugh the learned things again), so thanks for this stuff, guys, you rock! 🙂
And, of course: the people who have invented and programmed screen, really rock! This program is awesome! Thank you very much! 🙂