The watch
command in Linux is used to run other commands on a regular interval, and then it displays the output in the terminal. Here’s how to use it!
Sometimes, while working on the Linux command line, you might want to execute a command repeatedly so as to track any change in output. Luckily, there is a command-line utility that lets you do this.
With the Linux watch
command, you can track the changes in the output from time to time. It is beneficial for reflecting the real-time view of events that are happening on an operating system.
The watch
command comes installed, by default, on nearly all Linux distributions. It is useful when you need to monitor changes in a command output over time. So instead of reading the whole output, you can keep an eye on the changes.
How to Use watch Command in Linux
The syntax for the watch
command is as follows:
watch [OPTIONS] COMMAND
Where:
-
[OPTIONS]
: Adding an option changes the way thewatch
command behaves. -
[COMMAND]
: A user-defined command you want to run repeatedly.
By default, the specified command will run every 2 seconds, and watch
will run until interrupted by the user (pressing Ctrl+C
).
For example, we can monitor the output of the date
command every two seconds:
watch date
As a result, the watch
command will temporarily clear all of the terminal content and start running the provided command at regular intervals. On the top left side of the screen header, you can see the update interval and the executed command.
How to Change the Time Interval
We know that by default, the command runs every 2 seconds. If we pass the -n
(--interval
) option to the watch
command, we can specify the update interval. You will need to specify the amount of time in seconds.
I’ll continue to use the date
command as an example. The following command will run date
every second.
watch -n 1 date
Highlighting the Difference
If you’re running a command that prints out a big output, keeping an eye for changes can become pretty troublesome. Fortunately, the watch
command can highlight the difference between the previous output and the current one.
To view the changing output we can use the -d
(--difference
) option. This option will highlight the changes.
watch -n 1 -d date
However, if the interval between the updates is very short, for example -n 0.1
, it will be challenging to review differences. Therefore, you must set a reasonable update interval.
Hide Header in Output
The -t
(--no-titile
) option is used to turn off the header showing the interval, command, and the current time at the top of the display, if you’re not interested in seeing this portion.
watch -t date
Exiting When Change Occurs
By default, the watch command keeps running until it is interrupted manually by the user (Ctrl+C
). However, sometimes instead of highlighting the changes, you’d prefer watch
to completely exit when a change is detected.
You can set watch
to exit when the output from the command changes by using -g
(--chgexit
) option.
watch -g date
The above example stops the watch
command any time there are changes in date
output, which in this case means after 2 seconds.
Beep on Error
The watch
command can also give a beep sound if an update fails. It uses the beep
package to play a sound alert if the output update fails due to an error.
watch -b incorrect-command
Conclusion
Now you know all about the watch
command on Linux. Although it’s a simple program, if you use it properly, it can be quite useful.
Find out more about various options in watch
in its command line manual page.