In Linux, a watch command is a powerful tool that allows users to monitor the output of a command in real-time. This can be particularly useful for tracking the progress of a long-running process, monitoring log files, or keeping an eye on system resources.
The watch command is a versatile and useful tool for anyone who wants to monitor the output of a command in real time on a Linux system.
This post will provide a deep insight into the working and usage of the watch command in Linux with the following outline.
Let’s start discussing the watch command in more depth.
What is Linux watch Command?
The basic syntax of any command can be learned by taking the help, which can be done as shown below:
$ watch -h
It can be seen in the above image that the basic syntax of the watch command is:
$ watch [options] command
The details about the different options will be discussed in the next section whereas you can use any command with it to check its status.
How to Use the Linux watch Command?
In this section, we will discuss different options associated with the watch command.
Show the Output Repeatedly
When you use the watch command without any flag or option then; it keeps on executing the command after every two seconds, which can be seen below:
$ watch date
Show the Output After Specific Instant
If you want to execute any command using your own desired interval. You can do that by using the ‘-n’ flag as shown below:
$ watch -n 10 date
The above command will keep on executing the date command after every 10 seconds.
Highlight the Changes
The watch command can highlight the changes you want to see in any command. One of its examples is given below:
$ watch -d ps
The last three values keep changing when the command executes after every two seconds, which is why it highlights those three values.
If you want to view the changes after your desired interval, then you can combine the ‘-d’ flag with the ‘-n’ as shown below:
$ watch -d -n 10 ps
This command will execute after every 10 seconds and will show you any changes as well during this occurrence.
Exit the Execution After Any Change
If you want to exit the execution after any new changes occur, then you can need to use the ‘-g’ flag as shown below:
$ watch -g ps
That’s how the watch command works in Linux.
Conclusion
In Linux, the watch command allows you to continuously execute a command and display the output on the terminal. This can be useful for monitoring the output of a command in real-time, such as tracking the progress of a long-running process, monitoring log files, or checking system resources.