Utilities

Linux watch Command

Sometimes, you might face the necessity of running a command repeatedly to identify if there’s any change in the output. Of course, certain job can be performed with the help of Bash scripting and other forms of scripting. However, Linux comes up with a built-in command-line tool that does the job.

In this article, we’ll be taking look at the “watch” tool in Linux. This tool will run any command you specify with time interval. We can also set the time interval for convenience.

“watch” version

This simple command will print out the software version of “watch”.

watch --version

Or,

watch -v

Basic “watch” usage

Here’s the basic structure required to summon “watch” and tell what its job is.

watch <options> <command>

For example, let’s start “watch” with another common command: “date”. This tool prints out the time and date of the instance of running the tool. Learn more on “date”.

watch date

What’s happening here? The tool is running the command “date” every 2 second. Remember, this is the default value of gap between each time “watch” runs the command. In this case, “watch” is waiting 2 after it calls “date”. Of course, the time limit can be changed.

Note: “watch” won’t terminate on its own. You have to manually send termination signal to stop the command from running anymore. Press “Ctrl + C” to terminate the process.

Highlighting the difference

Suppose, you’re running a command that prints out a BIG output. Keeping an eye for changes can become pretty troublesome in that case. How about letting “watch” do the job?

Yes, “watch” can highlight the difference between the previous output and the current one. For enabling this feature, use the “-d” flag at the time of calling “watch”.

watch -d date

As you can see, the changes in output is highlighted. You can easily keep track of the changes. However, if the time in-between each run of the command is too short, you also might end up confused and lost. So, make sure that your time interval is on the sweet spot.

Update interval

As mentioned before, “watch”, by default, applies 2 second as the time interval between each run of your selected command. Thankfully, we can manually change the interval and set different value. For this purpose, we have to use the “-n” flag followed by the number of seconds.

For example, let’s run “date” with 10 seconds of interval.

watch -n 10 -d date

Note that I sneaked in the “-d” flag. This offers an easier way of catching the difference. Of course, you can always check the effective time interval at the top of the running console.

One thing to notice that this command allows precision only up to 0.1 seconds. You go smaller than that, it won’t accept it.

Turn off header

When you’re running any “watch” command, you’ll end up with the header containing essential info like the time interval, system name, system time etc.

However, if you’re not interested in seeing this portion, it’s possible do so. All you have to do is add the “-t” flag when running “watch”.

watch -d -n 10 date

watch -t -d -n 10 date

This can make a big difference if you’re working with something critical and don’t want any distraction or to save a little screen space.

Exiting when change occurs

This is an interesting one. Most of the time, whenever you’re working on something that requires constant checking, you might want to keep on monitoring its activities. However, there’s this feature embedded in “watch” that tells the program to exit once it identifies any change in the output.

For this purpose, use the “-g” flag.

watch -g -n 10 date

After 10 seconds, “date” command would report a different output. As output change occurred, “watch” exited.

Exit status

When “watch” exists in such instance, it reports certain flags. These flags are integers, ranging from 0 to 8 with different meanings.

For example, let’s run this command and let “watch” terminate itself automatically.

watch -g -n 10 date

Now, as the program exists, it returned a certain exit status. Run this command to check the exit status.

echo $?

Now, what does this value mean? To learn more about the values, you should check the man page of “watch”.

man watch

Parsing BIG commands

If you’re interested in a cleaner look for running “watch” commands, there are various ways we can achieve that.

First of all, use the \ sign after “watch” and all its flag, then hit Enter.

watch -n 10 -d \

Now, type whatever command you want to be repeated.

Another interesting way is quoting your entire custom command with single quotation marks ”.

watch <options> '<commands>'

Final thoughts

There are not a lot of things going on with “watch”. It’s a simple program with simple flags and simplistic behavior patterns. However, they can perform really wonderful job if used in the correct manner.

To know more about all the flags and their explanations, feel free to check out the man pages of “watch”. If you’re doing something critical, then it’s all worth the effort.

man watch

watch --help

Enjoy!

About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.