Watch Command in Linux is a useful command which is present in most of the Linux Based Systems. It runs a command at regular intervals and displays its output. It is useful when you have to run a command repeatedly and watch the output for any changes. For example, you can use this command to continuously monitor the free memory in your system.
In this article, we will discuss the Linux Watch Command. Also, we will discuss a few examples of using it.
watch [OPTIONS] command
Using the Watch Command in Linux
Let’s discuss a few ways of using the command.
Simple Example of Watching Date
In order to understand the usage of the command, let’s run the date command in terminal.
$ date Wed Jun 19 19:02:50 IST 2019
Now, the date command simply prints the current date with time on the terminal. What if you want to continuously want to monitor the change in the output of the command. You can simply run the command as a parameter to the watch command in Linux. For instance, consider the following example:
$ watch date
As it can be observed in the terminal, the watch command temporarily clears everything from the screen and provides the output of the date command at regular intervals. Also, by default the command, the command runs the specified command every two seconds (Every 2.0s: date). This can also be observed by the header at the top left corner of the screen.
Also, you can exit the command by pressing Ctrl + C in terminal.
Exiting the Command on Any Update
Also, you can exit the command on any update using the -g (–chgexit) option. In this way, the watch command in Linux will exit as soon as any value on the output changes.
$ watch -g date
You can also disable the top left header of the command using -t (–no-title) option in the watch command. For instance, consider the example below:
$ watch -t date
Wed Jun 19 19:10:29 IST 2019
By using the -t option, the command does not display the top left header in the output screen.
Change Time Interval
By default, the command regularly updates the output every 2 seconds. You can also change the time interval by passing the -n (–interval) option. For instance, if you want to constantly monitor the free memory every 1 second, run the command like below:
$ watch -n 1 free -m
In the above example, the Linux Watch Command runs the free -m command every 1 second and prints the output.
watch -n 5 <COMMAND> #Run Command Every 5 Seconds
watch -n 10 <COMMAND> #Run Command Every 10 Seconds
Highlighting the Changes in Every Update
Similarly, you can highlight the changes after every successive update in the Linux Watch Command using the -d (–difference) option. For instance, you want to monitor the Load Average using the uptime command.
$ watch -d uptime
The command highlights the changed values after every subsequent updates.
However, if you want to the highlighted portion to remain if that value was changed even once, pass the cumulative option along with -d. The updates will remain sticky in this case.
$ watch -d=cumulative uptime
Running Multiple Specified Commands
Similarly, you can run multiple commands separated by Pipe ( | ) in the Watch Command in Linux. However, you need to write the command in a single or double quote as below:
$ watch "COMMAND1 | COMMAND2"
For instance, the following command keeps displaying any running processes with java using ps and grep command together.
$ watch "ps ax | grep java"
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.