Thursday, October 11, 2012

SCREEN

Many of us know the joys of the "Screen" command. I did notice that this topic became a point of many questions at the recent Ohio LinuxFest, so I thought I could touch more on it here.

Being able to detach and reattach to a shell is a wonderful thing.  It is very useful when executing long query transactions like MySQL data exports or even running backups at times. It is also very useful to be able to change computers or locations and keep the same working shell environment. I often used Screen when I started a shell script that executed a full compressed backup, scp the files to a remote slave, and then would start to uncompress the backup onto the new slave. The entire process for this was around 24hrs easily so being able to reattached and monitor the progress was very helpful.

Being able to have a visual display that is quick and clear as to what host your connected on is of course critical. A fast an easy way to know MySQL Hosts is also very valuable.  At the very least you can change the mysql prompt in the my.cnf file
(   ie my.cnf --> mysql --prompt=" this is a mysql box>"  )

or just via your session

( mysql> prompt MySQL Master>
PROMPT set to 'MySQL Master> '
MySQL Master> show master status\G

). 


Screen becomes helpful keeping the shells organized. Personally, I do not like to have a different colors in my shell backgrounds.  A simple white or black is just fine for me.


Here are a few other things I do with Screen:

1st  create or edit a file for yourself vi ~/.screenrc

I like to enhance my status line at the bottom with the following:

hardstatus string '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]%{=b C}[ %m/%d %c ]%{W}'

You end up with something like the following on the bottom of all screen windows.

[ Remote Hostname ][            (0*$ bash)                             ][1.00 1.00 1.00][ 03/07  0:20 ]

if you have multiple screens you get this:

[ Remote Hostname ][          (0*$ bash) (1*$ bash)            ][1.00 1.00 1.00][ 03/07  0:20 ]

Yes you can have multiple screens just type screen when your inside a current screen to start another.

It is always best to name your screens as well.

Cntrl A then :
Then type "Title < title name>" of what you want to label the tab window. Your results are similar to below.

[ Remote Hostname ][    0-$ MySQL_Master  (1*$ MySQL_Slave)        ][1.00 1.00 1.00][ 03/07  0:25 ]

Then Cntrl A <0--9> to switch between tab windows.

You can access a screen session via any computer that is allowed to connect to the screen host. So always keep in mind to lock your screen sessions.

Cntrl A password  -- So you can lock with a password of your choice.

You might have several different Screen sessions so try to also name your screen sessions.  You might be executing a MySQL dump in one Screen session and working with a data export in another.

ie:
screen -S MySQL_dump
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.

This naming comes in very handy when you have to reattach.
I often use the handy shell script found here: https://github.com/cep21/jackbash/blob/master/bin/screens


Another big advantage of Screen is being able to work with remote workers in training or just reviewing MySQL queries.  For example, you can have a few different developers on the same screen session and execute explain on their recent queries so you can all review the best index options together.
If you want to learn more about that here is a nice article.



Some good tips on screen are also available here
http://www.softpanorama.org/Utilities/Screen/tips.shtml
http://polishlinux.org/howtos/screen-tips-tricks/