Sunday, March 6, 2011

Linux SSH and the Screen command

The screen command, what is that you ask?
That is a good question. The screen command comes standard with most linux distributions. Ok, So what does it do you ask? Well it is quite simple and extremely handy to know.

Given scenario: You are tasked with copying a large amount of files from an external USB drive to one of your linux file servers. The server has no GUI installed, so you have to use the CLI. So you fire up putty and mount the drive and then you start the file copy. It is close to time for you to leave the office for the day. You know that if you close the putty session, your file copy will stop. This sucks, you figure you still have a couple of hours to wait before the copy finishes. You need to leave, and the files need to be in place by the time you get to the office in the morning. This is where screen come to the rescue.

man screen
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSIX3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.

Instead of immediately starting yout copy session once you have your putty session established, try this.
Start your putty session, then just type in the command "screen". You will be shown an intro window, just press space to make that go away. You now see your command prompt. Start typing, start your file copy.

Oh, you just found out you need to modify your firewall and the file copy hasn't finished yet, no worries, screen can assist you here too. Instead of opening a new putty session, just press ctrl+a and the c. This will create a new session. You will see a new prompt appear, start typing away.
While watching the logs to make sure you opened up the correct ports, you need to check on your file copy, simple. Press ctrl+a and the p to return to your previous session. To go back, press ctrl+a then n to hop in to the next session you created. Seems simple enough.

Now it is time for you to shut down for the day. Press ctrl+a then d to disconect from screen. You will return to your original session where you can then safely exit putty.

You make the commute home. Once home all safe and sound, you decide to connect to the office and check your file copy. Once again we fre up putty. Now we just need to reconnect to your screen session. Just type in screen -r. You will connected back to your screen session.
Well that was easy. There is much more functionality in screen. This is enough to get you going, and I highly recommend you spend some time looking around in the man pages to see what else you can with screen.

So leave a comment and let me know what you think. How has the screen command helped you?

No comments:

Post a Comment