Using watch to monitor other commands periodically

Lately I’ve been doing a lot of hardware diagnostic tasks. One of which was to monitor network connection over screwed (and then repaired) twisted pair cable. I found myself constantly hitting to call ip l show eth0 again. And then I thought…

Use the loop

Something simple enough like

while true; do ip l show eth0; sleep 1; done

would do. Luckily, on the morning I’ve read about one useful utility called watch.

watch

It does essentially the same, plus it has some nice features like:

  • it outputs a single result of the command passed, so the thing going looks a lot like top. Neat;
  • it has -d flag, which highlights diffs between subsequent results;
  • it can beep on non-null exit status with -b.

So the command above would look like

watch -n 1 -d ip l show eth0

and it also would highlight the line eth0 <NO-CARRIER BROADCAST...> eth0 <BROADCAST ...> with inverse color when it changes, so it becomes quite noticeable when the link is lost.

SHOW COMMENTS (0)