Friday, October 16, 2009

GNU Screen

The concept of GNU screen is simple. It allows you to have multiple terminals open in a single session that can persist through sessions, disconnects, and logouts.

GNU Screen solves a lot of problems commonly encountered by professionals, having trouble staying connected to a system with a bad link? Screen your session and just reattach and never lose the work you've already done.

Want to continue work from home that you started at work? Screen your session, disconnect when you are finished at work, and reattach when you get home and continue where you left off.

Additionally GNU screen can let someone else watch, and interact, with your screen while you perform work for them making it a great tool for training new administrators.

GNU Screen works on most Unix, Linux, and even MacOS X, so it's a versatile tool that is available almost anywhere you might find yourself working. Lets get started using screen...

Verify GNU screen is installed and if not install it however your distribution suggests.

To use screen type: screen

Depending on the exact default options on your system this may display an information page about screen that you need to use a key press to dismiss or this may simple appear to do nothing except bring you back to the prompt.

To see if your screen is running you can type: screen -list

This will return a display of all running screens you have access to and will look like this:

server:~# screen
server:~# screen -list
There is a screen on:
26512.pts-0.core (10/16/2009 02:54:42 PM) (Attached)
1 Socket in /var/run/screen/S-root.
server:~#

The two most relevant pieces of information are the process ID (26512 in this case) and the status of the screen (Attached) in this case. What this means is you are currently interacting with this screen session. Lets start a simple process in the screen to demonstrate what it can do. Type the following:
for i in `seq 1 17280`; do echo $i; sleep 5; done

Normally, if you sat and watched this extremely entertaining count from 1 to 17280 it would take approximately 24 hours... but as exciting as it may be, you have other things to do and need the count to finish for you, so what can you do? Since we're running in a screen we can detach from the screen session.

Press CTRL-a d and you will be sitting back at the prompt.

If you issue a screen -list now you will see the following output:

server:~# screen -list
There is a screen on:
26512.pts-0.core (10/16/2009 02:54:41 PM) (Detached)
1 Socket in /var/run/screen/S-root.
server:~# screen

The "detached" status means the screen is still running and the time is still going forward just like you were watching that exciting count, but you are free to do other things. You can start another screen session too just in case you find yourself needing to leave your current project and come back to that at a later date. So type screen again. Just for demonstration sake, go ahead and hit CTRL-a d to detach this idle screen also.

Normally if you type screen -r and only have a single screen running you will automatically reattach to the running screen. Which is pretty convenient. However if you have multiple screens running screen needs to know which screen you want to attach to so lets take a look at the how to do that first lets list our screens.

server:~# screen -list
There are screens on:
27296.pts-0.core (10/16/2009 03:11:12 PM) (Detached)
26512.pts-0.core (10/16/2009 02:54:42 PM) (Detached)
2 Sockets in /var/run/screen/S-root.
server:~#

The start time on the screens tell us which is which and since it's been a while lets check on our exciting count... In my case we would type:

server:~# screen -r 26512

and what you'll see is a screen full of numbers... the count has been progressing without us and its showing just like we had never left the terminal in the first place. Detach the screen by pressing CTRL-a d.

That brings us to an issue with screen... who can possibly remember what and when they started each screen process and for what reason. Well screen lets you name its sessions!

If you're starting a new session and know what you want to use it for you can simply start screen like this:

server# screen -S mail

which would give our screen session the name of 'mail', however, often as not it's more useful to name a screen that is already in use. Lets give our count session a name:

server:~# screen -r
There are several suitable screens on:
27620.mail (10/16/2009 03:18:02 PM) (Detached)
27296.pts-0.core (10/16/2009 03:11:12 PM) (Detached)
26512.pts-0.core (10/16/2009 02:54:42 PM) (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
server:~# screen -r 26512

The count of the numbers displays on the screen and i press: CTRL-a :sessionname count and then I detach my screen again via CTRL-a d.

Now when I perform my list of screens I see the following:

server:~# screen -list
There are screens on:
27620.mail (10/16/2009 03:18:02 PM) (Detached)
27296.pts-0.core (10/16/2009 03:11:12 PM) (Detached)
26512.count (10/16/2009 02:54:42 PM) (Detached)
3 Sockets in /var/run/screen/S-root.
server:~#

If I wanted to resume count or mail it would be as simple as typing screen -r count or screen -r mail.

The way you kill a screen is simple, attach to it, and type: exit or press CTRL-d on most versions of Linux and Bash. If necessary you can also kill it by executing a screen -k PID

If as root you wanted another person logged in as root to review something in your attached screen session they could do so by typing: screen -x and they would see what you were seeing and could type into your session. This is sometimes useful when showing someone an error you don't want to repeat or having them give you assistance when you're stuck but need training on the particulars of the item in question.

I have only barely touched on the most basic of screens functions, it can open multiple windows inside itself (think tabs in a browser), it can be attached locally or remotely, it can run alternate shells, it can log its session, and many, many, many more things. GNU Screen is one of the most versatile tools in *nix for administration and general use.

There is a lot more to screen than the basic usage I've covered and you can discover a lot of additional short cuts and functionality by reading the screen man page (man screen). Use it before long compiles, use it before system updates, use it before anything that might cause major issues if you are disconnected midway through and GNU screen will quickly become a tool you can't live without!

1 comments:

  1. Man, this post was great! Thank you very much! ;)

    ReplyDelete