In Linux, you can use the “watch” command to run a command periodically at a predetermined interval and show the results on the screen. This can help keep tabs on a process’s progress or the status of a system resource, including memory utilization, network activity, or disc space. It may also be used to look for modifications to a log file or fresh files in a directory regularly. The “watch” function enables you to track changes over time and immediately see any problems or patterns, making it ideal for monitoring and debugging. The watch command in Linux and all of its options will be shown to you in this article.
Syntax:
Let us start with the idea of the basic syntax of the “watch” command that is shown below. The command argument is the command that you want to execute and the options argument is used to modify the behavior of the watch command. By default, the watch utility will run the stated instruction every 2 instants and show the output on the terminal.
Check Version
The command watch –version is used to display the version of the watch command that is currently installed on the system.
The output of this command tells you that the version of the watch command installed on the system is 3.3.17 and it is from the procps-ng package.
Watch “-n” Option
When you run the watch date command, the current date and time will be displayed on the terminal. The command will execute again and display the updated date and time after 2 seconds by default if you do not use the “-n” option here. This process will continue indefinitely until the watch command is manually stopped.
After 2 seconds, the output will be updated like this.
The watch command also has several options that can be utilized to alter its behavior. One of the most useful options is the -n option, which allows you to specify the interval at which the command should be executed. For example, to execute the date command every 5 seconds, you can use omar@virtualbox:~$ watch -n 5 date
The command:
The df command (short for “disk free”) shows the amount of free space available on the file system(s) that are currently mounted on a system. The -h option (short for “human-readable”) is used to display the sizes of a file system in a more human-readable format, such as GB or MB, instead of blocks. When you run the watch -n 2 df -h command, the output will show the file system name, the total size of the file system, the used space, the available space, and the percentage of used space, as well as the mount point of the file system. The command will execute again after 2 seconds and display the updated information.
Watch “-d” Option
Another useful option is the -d option, which highlights the differences between the current output and the previous output of the specified command in the “watch” instruction. This can be convenient for observing changes in output over time. For example, to monitor the uptime of your system and highlight any changes, you can use it. The output will be color-coded with any changes being highlighted in white.
The uptime instruction is used to display the current time, the amount of time the operating system has been executing, the total number of consumers presently logged in, and the mean load on the operating system for the previous 1, 5, and 15 minutes. It can be useful for identifying performance issues or for monitoring the performance of a system under heavy load.
The date command in Linux is used to display the current date and time or to set the system date and time as shown below.
When you run the watch command following the “date” command preceded by the “-d” option, the current date and time display will be shown as demonstrated below.
The command will get executed once again and show the updated date and time after 2 seconds by default, highlighting any differences in the output. The output below shows the output after several chunks of 2 seconds.
The watch command is useful when you want to monitor the output of the free command over time, such as monitoring the memory usage of a system. This can be useful for troubleshooting memory-related issues or for monitoring the performance of a system under load. When you run the watch free command, the current memory usage information will be displayed on the terminal. The command will execute again and display the updated memory usage information after 2 seconds by default.
The output of the free instruction will typically include information on the full sum of memory, the total of used space, the total of free space, and the total of space utilized as buffers or cache. It will also typically include information on the total amount of swap space and the amount of used and free swap space.
The -m option is used to display the memory usage in megabytes.
Watch “-p” Option
Here comes the -p option of the watch instruction that is used to make the watch command display the output in a continuously scrolling manner. So, the oldest lines will be deleted as the new ones are added.
Watch “-t” Option
The -t option is just one of the many options available to customize the watch command to suit your needs. The -t option is used to remove the header (which shows the command being executed and the refresh rate) from the output of the watch command.
Conclusion
This guide explains the use of “watch” instruction to monitor different activities in Linux with several options in its introductory paragraph. We have explained the way of using it in Linux shell using its syntax illustration. After that, we came up with several different and specific-purpose options for watch commands used to monitor different instructions. The options of watch command we have covered in this guide are -p, -d, -t, and -n.