Friday, December 24, 2010

The screen function in linux


In unix like operating systems we have the 'screen' utility, so usefull as easy to use. In cases where you need to compile a kernel in the background in a remote server (where the connection can get lost), download a file or do some critical operation without the risk of loosing the network connection to the server, the 'screen' utility comes handy.

Run it just writing its name:
#screen

This creates a virtual shell, a virtual terminal, for those (like me) that love virtualization :-)
Now you see a new shell, type some commands, leave a 'top' running, and type:

ctrl+a+c (this creates a new window, like a new shell).

How to go back to the window with the 'top' ?
Type ctrl+a+n (this jumps to the next window, in this case, the first one).

I dont know of a limit of shells you can create with "ctrl+a+c".
You jump backward to the previous window with "ctrl+a+p".
Split the current 'screen' window in multiple windows with "ctrl+a+S"
Jump between splitted windows with "ctrl+a+TAB".

And, if you need to send the the terminal the "ctrl+a" signal, press "ctrl+a+a".

Now... the real magic, how to leave this process running in the background and how to recover the terminal after a connection loose ?

Press ctrl+a+d to disconnect from 'screen' and leave all the terminals in the background, these processes will survive as i said, a disconnection, so you can disconnect from that server and come back in two days that screen will be still there.

Out of 'screen' write:
#screen -ls

This shows something like:

There is a screen on:
31696.pts-2.gw0 (Detached)
1 Socket in /tmp/uscreens/S-wlamagna.

To re-connect to screen write:
#screen -r 31696.pts-2.gw0

split display: C-a S
jump to next display: C-a tab
remove current region: C-a X
remove regions but the current one: C-a Q

And you are back in the screen terminals.
I hope it was usefull, please leave me a message with your experience or new tricks.

No comments:

Post a Comment