GNU screen utility for DBAs

Posted in: DBA Lounge, Oracle, Technical Track

One of my recent blogs was dedicated to rlwrap utility used in my daily life as a DBA. I want to follow the subject and introduce, or maybe bring back, another extremely useful program. Let’s imagine you are working on an environment in ssh console and need to start a command interactively. The problem is the network is unstable, the execution is going to take some time, and any interruption may potentially lead to an inconsistent state for your application or data set. Sounds familiar, doesn’t it? It can be activities like applying a patch to your database home, or executing a script cleaning old data and inserting new. Of course you can create a job or script and run in background mode. But what if we had another option? It is called “GNU screen” and I have used it for many years. It is a terminal window multiplexer with many useful options. Let’s have a look and try it out.

Where can we find it? The utility included to standard yum repository for Oracle Linux, Red Hat and, I believe, for other Linux distributions too. So, let’s install the utility and see what we can do with it. The installation is simple :

[[email protected] ~]#yum install screen
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
...........................................
Installed:
  screen.x86_64 0:4.1.0-0.23.20120314git3c2946.el7_2
Complete!
[[email protected] ~]#

Simple like that. Now we can start our first screen session. Just type “screen” and press enter:

[[email protected] ~]$screen

You are getting the usual terminal screen and can run your program from there. Everything looks exactly the same from the first glance, but in reality you are now inside your “GNU screen” session.
If you are somehow disconnected from the network you can start a new ssh connection and attach back to your running screen session. It will be still running and there you will see exactly the same session you suddenly left before. Or you can detach yourself from the screen session just pressing “Ctrl-a” and “d” . By default the prefix for all screen commands inside a screen session is “Ctrl-a”. You can change the default prefix defining the “escape” parameter in /etc/screenrc or ~/.screenrc file. Also you may use the command “screen -e ” to start your screen with another prefix (meta key) if you don’t like ctrl-a.

You can list and find your session later using screen -ls or checking the directory /var/run/screen/. Of course the “screen -ls” is much simper.

[[email protected] ~]$screen -e ^bb
[detached from 5959.pts-0.sandbox]
[[email protected] ~]$screen -ls
There is a screen on:
	5959.pts-0.sandbox	(Detached)
1 Socket in /var/run/screen/S-oracle.
[[email protected] ~]$ll /var/run/screen/S-oracle/
total 0
srw-------. 1 oracle oinstall 0 Aug 25 10:08 5959.pts-0.sandbox
[[email protected] ~]$

To attach yourself back to the session you can just run “screen -x”(if you have only one running screen session) or “screen -r “:

[[email protected] ~]$screen -r 5959.pts-0.sandbox

And, of course, you can have several different screen sessions running simultaneously. You may like to name them starting with parameter -S and one can be for patching when the other can be for a completely different activity.

[[email protected] ~]$screen -S patching
[detached from 8852.patching]
[[email protected] ~]$screen -S clean_data_script
[detached from 8867.clean_data_script]
[[email protected] ~]$screen -ls
There are screens on:
	8852.patching	(Detached)
	8867.clean_data_script	(Detached)
2 Sockets in /tmp/screens/S-oracle.
[[email protected] ~]$

You can start your patching when you are at the office and continue your work later attaching to exactly the same screen from your home. I think it is brilliant. Now you are not pinned to one place and have flexibility. Also your most important activity doesn’t depend on an unstable internet connections.
As a bonus you may ask somebody to attach to the same session and, as result, enjoy collaboration with your teammates troubleshooting a problem or sharing your experience.

Also keep in mind screen is a terminal multiplexer. What it gives us is the ability to fire up several terminal windows inside one screen session and switch between them. Inside the screen session you press Ctrl-a c to create a new window or windows and switch between the windows using Ctrl-a n (you can find other options in the documentation for screen).

One more thing you may like is the ability to split your screen to two or three parts and have, for example, running a sqlplus session, tail for an alert log output and a performance tool at the same time like :
Screen Shot 2016-08-25 at 10.52.48 AM

An extremely useful option for those who wants to script some actions is the ability to send a command to any of your running screen sessions using the “-X” parameter. For example you can send command “top” to a screen session “patching” to page 3 using :

[[email protected] ~]$ screen -S 6236.patching -p 3 -X 'top\r'

I received some useful advice from my colleagues while I was writing this blog post. The first one was about using the option “-L” which enables output logging for your screen session. You can also enable it inside your screen pressing ctrl-a H. It may come handy if you want to see what was happening when you was detached from the session.
The second piece of advice was about solution or workaround when your session cannot start when running on a pseudo tty :

[[email protected] ~]$ su - oracle
Password:
[[email protected] ~]$ screen
Cannot open your terminal '/dev/pts/0' - please check.
[[email protected] ~]$

What you can do is to use the “script” command and redirect output to either “/dev/null” or to another location if you need it.

[[email protected] ~]$ script -c 'screen' /dev/null
Script started, file is /dev/null
[detached]
Script done, file is /dev/null
[[email protected] ~]$ screen -ls
There is a screen on:
	30932.pts-1.sandbox	(Detached)
1 Socket in /var/run/screen/S-oracle.
[[email protected] ~]$

The workaround may help you when you are connected to a server with personal account but need to create a screen session for the oracle user or for any other user you are switching to by the “su” command.

As I’ve said, you can easily install the utility from your yum repository. But if you want the latest and greatest, you can try to download it from https://ftp.gnu.org/gnu/screen and compile by yourself. It may help if you hit a bug or find some new useful feature in the changelog for the latest version. When I was writing the blog the version in Red Hat repository was 4.0.1 vs 4.4.0 on gnu.org .

I hope you will find this utility useful for you. Explore, enjoy, and stay tuned. In one of my next blogs I plan to write about another utility with similar options.

email

Author

Want to talk with an expert? Schedule a call with our team to get the conversation started.

About the Author

Regarded by his peers as an Oracle guru, Gleb is known for being able to resolve any problem related to Oracle. He loves the satisfaction of troubleshooting, and his colleagues even say that seeking Gleb’s advice regarding an issue is more efficient than looking it up. Gleb enjoys the variety of challenges he faces while working at Pythian, rather than working on the same thing every day. His areas of speciality include Oracle RAC, Exadata, RMAN, SQL tuning, high availability, storage, performance tuning, and many more. When he’s not working, running, or cycling, Gleb can be found reading.

10 Comments. Leave new

Thanks, Gleb, for sharing experience. It’s indeed very useful tool for any Oracle DBA.

Reply
Gleb Otochkin
August 31, 2016 8:38 am

Hi Bair, thank you for the response. Interesting that we have a rival utility doing almost the same. I am thinking to do some comparison.

Reply

Hi!

One tip: I found that on screen page scrolling isn’t working, so solution for me was to turn on history (Ctrl+a+H), which outputs screen to file named screenlog.0. Then you can look into that file and scroll back with your favorite viewer.
Another way would be to use mosh instead of ssh to connect to your server.

Reply

Hi Kresimir,
Thanks for the comment. Good tip if you want to keep your scroll buffer saved in the file.
You also can try C-a to scroll back if I don’t really need to save your scroll buffer.

Cheers,
Gleb

Reply

You think Ctrl+C+a or some other combination of keys?

Reply

Sorry, looks like part of my comment was wiped out by the blog engine. You may use Ctrl+A ESC to scroll back.


Gleb

Reply

Yes, this works great, thank you!

Hi There,
#enable mouse scrolling and scroll bar history scrolling
termcapinfo xterm* [email protected]:[email protected]

add this to .screenrc file in your home and scrolling works as normal terminal.

Reply

That works too but I’ve found it bit messy when you work with several windows. Maybe it is only my experience. I prefer Ctrl+a Esc but it is just my preference.
In my .screenrc I have caption and hardstatus in the bottom. It has been taken from one blog long time ago but I don’t remember where.
caption always “%{+b rk}%H%{gk} |%c %{yk}%d.%m.%Y | %72=Load: %l %{wk}”
hardstatus alwayslastline “%?%{yk}%-Lw%?%{wb}%n*%f %t%?(%u)%?%?%{yk}%+Lw%?”

Reply

You really got to start looking into tmux, like yesterday. A way better tool to handle those long winded database refreshes.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *