Solved

Using colors in terminal with 'watch' command

Posted on 2004-09-10
4
242 Views
Last Modified: 2010-04-21

We are monitoring an application remotely (over ssh) by viewing changes in files that it creates.  Some files are  rewritten by the application continually, and we watch them using:

watch -d -n1 cat [filename]

The "-d" option highlights the changes occuring, and provides a simple but effective monitoring screen.

Is there any way to incorporate colors into this?  The application could insert special characters into the files.

In the xterm, typing "ls --color" highlights some files in different colors.  But "watch ls --color" doesn't show the colors.  Why is that?

Any creative ideas?  Alternatives to watch?  

thanks,
Dan Evison
0
Comment
Question by:devison
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
aib_42 earned 500 total points
ID: 12037539
Apparently, watch strips the ANSI escape character from its output.
(watch ls --color shows a lot of "[xxm" strings, which is the ANSI coloring sequence minus the starting \033)
I think this is because watch itself uses ANSI sequences (to highlight the differences and also clear the screen), and doesn't want programs to interfere with. Unfortunately, there seems to be no "--no-strip" parameter, either.

One simple alternative is to use

while true; do clear; cat [filename]; sleep 1; done

which doesn't really highlight the differences. Highlighting could be done by tee'ing the output to a file, comparing it to the last output and somehow color-drawing the differences onto the screen, but I think the best (and the easiest) alternative is to modify watch itself.

Actually I might do that myself. Not right now, though; I will post again tomorrow.
0
 

Author Comment

by:devison
ID: 12072603
Thanks for that information - I'm going to accept your answer.  I'd like to avoid the loop you suggested partly because I think watch is more efficient using the ncurses refresh function and perhaps only sending information that has changed.
I've had a look at the source code for watch myself, and I can see how I could change it to recognize specific escape sequences, then translate those back to ncurses function calls.  That doesn't seem ideal though.  
Any further thoughts on this would be appreciated.

Dan
0
 
LVL 7

Expert Comment

by:aib_42
ID: 12072997
> I will post again tomorrow.

D'oh, it's been 5 days. You can see how good of a week I'm having :).

Anyway, why wouldn't a modified watch be an ideal solution? What if it had a switch to preserve color? What if it was modified to have a switch to preserve color? :)
0
 

Author Comment

by:devison
ID: 12114206
Sorry, I missed your post...

A modified watch would be an ideal solution!  (The loop I mentioned I'd like to avoid was the while loop).  

I'm just wondering if watch can be modified without checking explicitly for every escape sequence and translating those back to ncurses function calls.  That's what I think may need to be done.

Of course it would be so much better to just pass any escape sequences over to the terminal, but because of the way watch writes character by character to specific coordinates I'm not sure that is possible.

What do you think?

thanks
Dan


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question