Posts Tips for using watch

Tips for using watch

Preview Image

I have been using watch for some time now but only recently I found out it offers some options that make it even better!

What is watch?

Watch is a program that repeatedly executes a command you tell it to and shows you the output. This can be very useful when you want to observe the output of a program change over time.

Useful options when using watch

Watch offers a couple of neat options.

Highlight differences

watch -d (or watch --differences) points out the changes between the latest command output and the previous one.

Whiptail message box

Cumulative mode

watch --differences=cumulative makes the highlights stick. In other words, all highlights remain visible and are never cleared.

Fun fact: you can actually give any value to this option instead of cumulative, as long as you provide a value to the --differences option.

Custom interval

watch -n <seconds> (or watch --interval <seconds>) allows you to specify with what interval your command should be executed. By default, your command/program will be run with an interval of 2 seconds.

This interval may be less than 1, but the minimum interval is 0.1s. Intervals less than 0.1 are capped at 0.1.

Precise timekeeping

The last option I want to mention is watch -p (or watch --precise).

This makes watch try its best to run the command at precisely the interval you chose.

Using a combination of the above mentioned options we can see this behavior.

Let’s try printing the precise timestamp every second:

watch -n 1 --differences=cumulative 'date --rfc-3339=ns'

Because we enabled cumulative mode we see what digits of the current timestamp change.

Whiptail message box

Now repeat the command with -p:

watch -p -n 1 --differences=cumulative 'date --rfc-3339=ns'

Whiptail message box

The tenths and hundreds units did not change, so the 1-second interval was better adhered to.

Does this really matter? That’s up to you to decide. At least now you know about the possibility.



This post is licensed under CC BY 4.0 by the author.