Solved

Using colors in terminal with 'watch' command

Posted on 2004-09-10
4
221 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
inotifywait in linux 2 279
Chinese translation in RedHat 10 46
php session folder question (Code Igniter) 3 72
Using init script with Systemd: need help crafting unit file 11 76
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now