4

There is a watch command that runs a program every N seconds, showing you the most-recent output and optionally highlighting characters that have been changed.

I'm often using it's --diff mode to see what changed since the last refresh interval, but then I have to make a tradeoff: do I want to see current data (use a low refresh interval) or have a good idea of how much of the output is changing (use a long refresh interval). Cumulative mode is too long, though. And the diff output is useless if a line is added/removed near the top, it'd be nice if that didn't highlight the entire screen.

Requirements:

  1. Runs on Linux, preferably in a terminal (CLI)
  2. Runs a command every N seconds (without time drift is preferred, but not required), and displays the output of that command.
  3. On the second, third, etc. refresh, highlights characters that have changed. Allows me to specify how many updates worth of changes I'd like to see (e.g., highlight characters that have changed in the past three refreshes, not just this one)
  4. Support UTF-8 reasonably well. E.g., understand that a character can consist of multiple bytes.
  5. Handle lines being added and removed without showing every character in every subsequent line as changed (e.g., attempt to resync like diff does, though using a different algorithm is fine)

Nice-To-Haves:

  1. Show how many refreshes ago the change was by using a different highlight color based on age. OK to require a 256-color capable terminal (e.g., to get a bunch of grays).
  2. Runs in a terminal, with no need for X11 (or Wayland).
  3. As mentioned above, mode to avoid time-drift (e.g., if you ask for every 30 seconds, and the command takes 5 seconds to run, it should still start it every 30s, not every 35s). [watch's -p option, which I wrote...]
  4. Support for formatting escape codes from the program being run (e.g., color, bold, underline).
  5. History, where you could look at previous outputs. For example, pressing h or left could show you the output of the previous run, pressing it again the run before that, then l or right could take you to the next run, and again back to the most recent run.
  • Your nice to have 3 could cause a problem if the process is scheduled for every 10 seconds but takes 30. – Steve Barnes May 14 '16 at 4:31
  • @SteveBarnes correct. I don't care much what happens then. Reasonable behaviors include running the command again immediately, repeatedly until it's caught up with the expected number of runs or waiting until the next scheduled run (just skipping the missed ones). – derobert May 14 '16 at 5:08
  • There would also be an issue with honouring the formatting escape codes and highlighting changes - e.g. is the word error red because it is a change or because it is red, same applies for most of the other ways of showing the changes. – Steve Barnes May 14 '16 at 6:38
  • @SteveBarnes yep, presumably the highlighting from showing differences would take precedence. Watch (or this improved one) is something you use on "friendly" commands, not malicious ones. It might also break of someone changes the clock, spews a gigabyte of output to it, attempts to switch the terminal to graphics mode, or pulls the power cord out of the wall. Mostly, the answer to these possibilities is "well, don't do that, then!" – derobert May 14 '16 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.