Linux Watch Command
In Linux, we use the watch command in order to execute the program at regular intervals, displaying output in Fullscreen. Watch command can run the definite command in the parameter constantly by displaying its output and errors. By default, in every 2 seconds, the specified command run, and the watch will run till interrupted.
Watch command is helpful if we need to execute the command continually and watch the output of the command change periodically. For example, the watch command can be used to monitor the system uptime and disk usage. Watch is pre-installed in almost every Linux distribution, and it is a part of the procps package.
How to Use Watch Command
If we want to use the watch command, then we have to follow the following syntax:
In order to understand how the watch command operates, we run the date command:
In the above image, we can see that the watch command will remove all the terminal content temporarily and begin running the given command periodically. If we use with no option, then the watch will run the specified command every two seconds.
We can see the watch update interval and the executed command, which is every 2.0s: date, at the top left of the screen header, while the clock at the top left watch displays the current date and time. If we need to stop using the header, we have to use the option -t (--no-title).
The specified command's output is shown on the screen and repeatedly updated in every two seconds.
We have to enter the Ctrl+C key if we want to exit out of the watch command.
We use this option to highlight the modification between two successive updates. The -d, -difference option read the alternative argument that makes the modification permanent, permitting the user to see what has been modified slightly once since the first iteration.
We use this option to determine the update interval. This command is not quicker to allow than 0.1 second interval, with the smaller values being changed.
Using this option, watch attempt to run the command every interval seconds.
With the help of this option, the header can turn off, which displays the command, current time, and interval at the screen's top.
If the command has a non-zero exit, then this option will give a beep.
The -e, -errexit option freezes the updates on command error and then exit after a key press.
The -g, -chgexit option exit if the command's output exit.
The -c, -color option interprets the style sequences and ANSI color.
This option is given to the sh -c that means we may need to use additional quoting in order to get the desired effect.
The watch -h option is used to display the help message and exit.
The watch -v option will show the information of the version and exit.