Putorius
Basic Commands

Watch Command - Monitor Command Output in Linux

Conceptual image of a magnify glass looking at a computer to depict usage of the watch command

Sometimes it necessary to run a command repeatedly every n seconds. For example if you are waiting for DNS to change you may run the dig command over and over to see when a change has propagated. It's easy enough to repeat the previous command in Linux using the bash history. The other option (see below) is to write a quick while loop. This is where the watch command saves the day.

With watch you can easily run a command periodically and monitor the output. The console is cleared which makes it easy to observe changes. Watch will run indefinitely or until it is stopped using the CTRL+C combination.

In this article, we will show you how to use the watch command and explore some of it's options.

Watch Command Basic Functions

By default watch will run the command given every 2 seconds. Here is an example of using the watch command to monitor the output of the dig command.

In the animated gif above you can see there is a header that shows you some basic information. On the left side it shows you the interval being used, in this case the default 2 seconds, and the command being run. The right side displays the host and the current date and time.

Change the Update Interval

To change the frequency of the output updates you can use the -n (--interval) option. This option takes the number of seconds as an argument.

Example of changing output interval to 6 seconds:

watch -n 6 "dig +short putorius.net"

Highlight Differences in Output

Using the -d (--differences) option you can highlight the changes to the output. This is very useful when waiting for a change. Below is a screenshot of the differences being highlighted.

When using the -d option, the differences will only be highlighted for that iteration of the command. On the next completion of the command, the highlighting will disappear. You can optionally use the "permanent" argument to allow the changes to stay highlighted.

Example:

watch -d=permanent "ls -lrt /tmp/ok"

NOTE: Some implementations of watch use -d=cumulative instead of differences, but in my experience both work on most Linux systems.

Exit on Error

Using the -e (--errexit) tells watch to exit if the command exits with a non-zero or error status.

watch -e "rgep failed /var/log/audit/audit.log"

As you can see I mistyped grep above, but luckily I used the -e option so watch won't continue running in error.

Remove Header and Only Show Output

Using -t option will turn the header information (Number of seconds, command and current date info at top of display) off.

watch -t "dig +short putorius.net"

Exit on Output Change

Using -g will tell watch to exit when the output of the command changes.

watch -g "ls -lrt /tmp/ok"

More Examples

Monitor the query time of a DNS server:

watch "dig @8.8.8.8 putorius.net | grep -i 'query time'"

Watching the size of a directory:

watch -n 0.5 "du -cksh /home/savona"

Conclusion

There are many ways to use the watch command and it is a very useful utility. We just scratched the surface here and outlined the most command options.

Leave me a comment below and let me know the interesting ways you have come up with to use the watch command.

Exit mobile version