Linux provides the
watch command in order to run specified commands at the specified periods regularly. The specified command is executed over and over again at the specified intervals and the output is generally printed to the terminal. We can run the disk, process, memory-related commands to monitor them regularly by using the watch command.
watch Command Syntax
The watch command has the following syntax where the command we want to execute is provided as the last parameter.
watch OPTIONS COMMAND
- OPTIONS is used to provide single or more option to the watch command. This is optional.
- COMMAND is executed regularly which also contains its options.
Display Help Information
Event the watch command has very basic features information about them can be displayed with the
-h option like below.
$ watch -h
Run Command Regularly
The watch command executed the specified command with 2 seconds intervals. In the following example we executed the
date command in every 2 seconds.
$ watch date
Run Command At The Specified Intervals
We can change the executions interval which is 2 seconds. The
--interval options can be used to set intervals as seconds. In the following example, we set the interval as 5 seconds.
$ watch -n 5 date
Alternatively, we can specify the intervals as milliseconds. In the following examples, we set the interval as 1.5 milliseconds.
$ watch -n 1.5 date
Cancel watch Command
The watch command runs forever unless it stopped the system shutdown. We can cancel or stop the watch command by using the
CTRL+C keyboard shortcut which kills the watch command.
Another useful feature of the watch command is comparing the outputs. The difference between current and last output can be diffed by using the
-d option. In the following example, we highlight differences in the date command.
$ watch -d date
Turn Off Header/Command Display
By default, the watch command displays the title or header in the output which is the executed command. This header displayed can be turned off with the
$ watch -t date