Introduction
To repeatedly run a command or job in regular time intervals while working in Linux, you can use cron jobs or bash scripts. However, Linux also offers a more straightforward, built-in solution - the watch
command.
In this tutorial, you will learn the watch
command syntax, how it works, and the different things it can help you do.
Prerequisites
- A system running a Linux distribution (learn how to install Ubuntu 20.04, how to install CentOS 7, or how to install Arch Linux)
- An account with sudo privileges
- Access to the terminal window/command line
Linux watch Command Overview
The watch
command is a built-in Linux utility used for running user-defined commands at regular intervals. It temporarily clears all the terminal content and displays the output of the attached command, along with the current system date and time.
By default, the watch
command updates the output every two seconds. Press Ctrl+C to exit out of the command output.
The watch
command is useful when you need to monitor changes in a command output over time. This includes disk usage, system uptime, or tracking errors.
Linux Watch Command Syntax
The watch
command uses the following syntax:
watch [option] [command]
Where:
[option]
: Adding an option changes the way thewatch
command behaves. Available options are listed below.[command]
: A user-defined command you want to run repeatedly.
The watch
command options include:
-n , --interval | Allows you to specify the interval between output updates. |
-d , --differences | Highlights the differences between output updates. |
-g , --chgexit | Exits the watch command when the output of the user-defined command changes. |
-t , --no-title | Removes the header showing the interval, command, and current time and date. |
-b , --beep | Plays a sound alert (beep) if the command exits with an error. |
-p , --precise | Attempts to run the command after the exact number of seconds defined by the --interval option. |
-e , --errexit | Stops output updates on error and exits the command after a key press. |
-c , --color | Interprets ANSI color and style sequences. |
-x , --exec | Passes the user-defined command to exec , reducing the need for extra quoting. |
-w , --no-linewrap | Turns off line wrapping and truncates long lines instead. |
-h , --help | Displays help text and exits. |
-v , --version | Displays version information and exits. |
Linux Watch Command Examples
Here are some of the ways you can use the watch
command options to achieve different results:
Run Command with a Custom Interval
Set a custom interval to run a user-defined command and show the output by using the -n
or --interval
option:
watch -n [interval in seconds] [command]
For instance, to display the system time and date every 5 seconds, run:
watch -n 5 date
Note: The -n
option allows you to use fractions of a second, with a minimum interval of 0.1 seconds. When entering decimals, both a period (.) and a comma (,) work for any locale.
Highlighting Changes Between Updates
Use the -d
or --difference
option to highlight changes between successive output updates:
watch -d [command]
For example, display the system date and time in the default 2-second interval with the changes highlighted:
watch -d date
Pass =cumulative
to the -d
option if you want all the values that have ever changed to stay highlighted:
watch -d=cumulative date
Exit on Change
The -g
or --chgexit
option causes the watch command to exit if there is a change in the output:
watch -g [command]
As an example, adding the free command monitors your system's memory consumption and exits if the value changes:
watch -g free
Hide the watch Command Header
Turn off the header containing the interval time, user-defined command, and current system time in the watch
command output by using the -t
or --no-title
option:
watch -t [command]
Returning to the example of displaying the system date and time, this time without the header:
watch -t date
Alert on Error
The watch
command uses the beep package to play a sound alert if the output update fails due to an error. To do this, use the -b
or --beep
option:
watch -b [command]
Note: if you don't have the beep package installed, add it with sudo apt install beep
command.
Using Complex Commands
The watch
command also allows you to use more complex user-defined commands, with their own arguments and options. One way to do this is to use the backslash ('\') symbol:
watch [options] \
Using the command above brings you to the next line in the terminal, where you need to add the user-defined command. Once you hit Enter, it executes the command. For instance:
watch -n 5 \
echo "watch command example output"
Another option is to add the user-define command in single quotation marks:
watch [options] '[command]'
Using the example above, the command would be:
watch -n 5 'echo "watch command example output"'
Conclusion
After reading this tutorial, you should have a better understanding of how the watch
command works and what you can use it for.
For a more comprehensive overview of commands, check out our ultimate list of Linux commands.