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
In this tutorial, you will learn the
watch command syntax, how it works, and the different things it can help you do.
- 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
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.
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
watch command uses the following syntax:
watch [option] [command]
[option]: Adding an option changes the way the
watchcommand behaves. Available options are listed below.
[command]: A user-defined command you want to run repeatedly.
watch command options include:
|Allows you to specify the interval between output updates.|
|Highlights the differences between output updates.|
|Exits the |
|Removes the header showing the interval, command, and current time and date.|
|Plays a sound alert (beep) if the command exits with an error.|
|Attempts to run the command after the exact number of seconds defined by the |
|Stops output updates on error and exits the command after a key press.|
|Interprets ANSI color and style sequences.|
|Passes the user-defined command to |
|Turns off line wrapping and truncates long lines instead.|
|Displays help text and exits.|
|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
watch -n [interval in seconds] [command]
For instance, to display the system time and date every 5 seconds, run:
watch -n 5 date
-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
--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
=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
--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
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
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
watch -b [command]
Note: if you don't have the beep package installed, add it with
sudo apt install beep command.
Using Complex Commands
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"'
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 Linux commands, check out our Linux command cheat sheet.