In the next article we are going to take a look at some ways to use the watch command. This command is used to execute any arbitrary command at regular intervals, displaying the result of said command in the terminal window. This can be useful when we have to run a command repeatedly and watch the command output change over time.
Utility watch is part of the procps (or procps-ng) package that is pre-installed on almost all Gnu / Linux distributions.
Useful examples of the watch command in Ubuntu
Use the utility watch it is a simple and straightforward task. Follow a simple syntax and no complex options.
watch [opciones] comando
To end the loop or repeat, you can use Ctrl + C to terminate the watch action, or simply close the terminal window where it is running.
Basic use of the Watch command
When used without arguments, this utility will execute the specified command every two seconds:
watch date
This command will print the result produced by date. The top left of the screen will show the command being executed and the active interval period.
Specify the update interval
We will be able to specify the interval period for updating the watch command very easily using the -n option. The new time interval must be set in seconds.
watch -n 5 date
Now the date command will only update every five seconds.
Highlight the differences between each update
Watch makes it easy to spot the differences between old and updated output. We can highlight these differences using the -d option.
watch -n 5 -d date
This command will run date every five seconds and highlight changes to the output on the terminal screen.
Remove title and headings
The watch command displays information on the screen such as the name of the command being executed, the interval, and the current time. Everything is at the top of the screen. If we want to avoid it, we can use the -t option to disable this information.
watch -t date
As I was saying, this command will only show the output produced by the command date.
Exit Watch in case of error
We can also specify a watch to exit whenever there is an error produced by the command that is being executed. We will simply have to use the -e option.
watch -e exit 99
If you run this command, you will see a message indicating that the command has a non-zero exit status. Keep in mind that the commands that are executed without any error, come out with a zero status code.
Exit if changes occur in command output
The -g option exits watch whenever there is a change in command output.
watch -g date
This command will run for two seconds and as soon as the output is updated, watch will close.
Notify in case of error
The -b option de watch beeps each time the command exits with a non-zero status code. As already mentioned, a non-zero status code usually indicates an error or that the execution of the command has failed.
watch -b exit 99
Interpret color codes and style sequences
We will can enable the interpretation of code codes ANSI color and the style sequences for watch using the -c option. By default, watch does not interpret colors in its output.
watch -c echo "$(tput setaf 2) Ejemplo para Ubunlog"
The output of this command shows the green encoded string ‘Example for Ubunlog‘. If we remove the -c option and run the command again, we will see that the string does not contain any color this time.
Monitor changes to directory content
The following example illustrates how we can use the watch utility to monitor file system directories for content changes.
watch -d ls -l
This command will print the directory listing and highlight changes to the content.
Monitor CPU temperature using watch
If you are using equipment that is heated, it is important to monitor the temperature. We will can use the watch utility together with sensors to control the temperature of the equipment.
watch -n 60 sensors
This command will check the temperature of the equipment per minute.
Show help page and manual
Do not doubt consult the help of the watch command if you want quick information for a specific option.
watch -h
We will also be able consult the manual page for detailed information on a specific option.
man watch
As seen, the watch command is a simple but useful tool, which has a good number of use cases, which are not all those shown in this article.