On several occasions, you may need to run a command or utility repeatedly after some time interval. We can use specific cron jobs with the help of bash scripting or using other programming languages. However, Linux has a built-in watch command that is used to run other commands on a regular interval, and then it displays the output in the terminal. The watch utility is pre-installed on nearly all Linux distributions.
Linux Watch Command with examples
In this tutorial, We will show you seven different ways to use the Linux watch command.
1. Run Command Every 2 Seconds
By default, the watch command runs after every 2 seconds and shows output on the terminal. It keeps running until interrupted by the user.
Syntax:
watch <option> <command>
Where <option> are the parameters which we will discuss later in this article, and <command> is another command that you want to execute by watch utility.
watch date
Example:
2. Run Command Every N Seconds
Watch command update interval can be overridden. It means you can change the update interval from 2 seconds to your desired interval as well.
Syntax:
watch -n <interval-in-seconds> <command>
Where <interval-in-seconds> is update interval in seconds and <command> is another command you want to execute by watch utility.
watch -n 5 date
Example:
3. Highlighting The Differences Between Updates
You can also highlight the differences between previous output and current output. So instead of reading the whole output, you can keep an eye on the changes.
Syntax:
watch -d <command>
However, if the interval between the updates is very short, it will be challenging to review differences, so you must set a reasonable update interval.
watch -n 5 -d date
Example:
4. Hide Header in output
Watch command output shows a header containing time interval, command, system name, and system date. However, if you do not want to see this portion, you can hide it as well.
Syntax:
watch -t <command>
watch -t date
Example:
5. Beep On Error
The watch command can also give a beep sound if an update fails. For this to happen, first, you may need to check if the beep package is installed or not. In case it is not installed, you can install it using the following command:
sudo apt install beep
Syntax:
watch -b date
6. Exit On Change
By default, the watch command keeps running until it is interrupted manually by the user. However, You can make it exit if it identifies a change in output as well.
Syntax:
watch -g <command>
watch -n 10 -g date
Example:
Now watch exit; it returns a certain status that can be got by using the below command:
echo $?
As you can see in the above screenshot, it displays certain integer flags starting from 0 to 8. Each of these flags has its meaning, and details of these flags are available on the man page.
man watch
7. Using Big Commands
You can use other commands with watch command in a cleaner way. There are multiple ways to do that. You can use \ sign after the watch command and its options.
watch -n 10 -d \
Example:
You can also write you complete custom command inside single quotation marks as well.
watch <options> '<commands>'
Example:
Conclusion
By now, you should have a good understanding of watch command and its importance, especially when you want to monitor your system’s resources like hard disk, network changes, CPU usage, etc. You can get more information about the watch command on its man page. Use command man watch in your terminal to view the man page.