Introduction to Linux Watch Command
In the Linux operating system, the watch command is used to run any command in the environment and print the output of the command on the working bash terminal or CLI. The major use of the watch command is to print the output difference in between the command i.e. if we are printing the same command multiple times and we need to check the output difference of the same command then we need to use the watch command.
In 1991, the innovative watch command was written by Tony Rems (email address: email@example.com) after that it will modify and corrected by Francois Pinard. After the time interval, there are lots of changes happen in utility.
watch [ options ] command
- watch: We can use the “watch” keyword in the syntax or command. It will take different arguments like options and the Linux commands. As per the provided arguments or options, it will print the command output on the screen.
- options: We can provide the different flags as the option that is compatible with the “watch” command.
- command: We can use the different Linux commands.
How does the Linux Watch command work?
In the Linux environment, we are having multiple ways to print the command in a specific interval of time. But in the watch command, we are printing the command output rapidly and observe the command output difference if any.
In Watch command below are the different exit codes available.
- 0: Once the job or command was successfully executed then it will print the “0” exit code.
- 1: Once the job or command failures then it will print the “1” exit code.
- 2: when the job or command forking the process to watch the failed then it will print the “2” exit code.
- 3: When it will replace the child process of stdout with write side pipe failed then it will print the “3” exit code.
- 4: When the command execution was failed then it will print the “4” exit code.
- 5: When we will closing the child process write pipe failed then it will print the “5” exit code.
- 6: When the IPC pipe creation failed then it will print the “7” exit code.
- Other: It will watch the command or job and will propagate command exit status as the child exit status.
Below are the lists of options that are compatible with the watch command.
|1||-d, –differences [permanent]||It will help to highlight the differences between successive updates on the job or command level. It will print the information or update if anything may change the successful execution of the command or job. What has changed at least once since the first iteration?|
|2||-n, –interval seconds||It will help to Specify the update interval.
Note: But the command will not allow quicker than 0.1-second interval in which the smaller values are converted.
|3||-p, –precise||It will make sure that the watch attempt to run the command every interval seconds. We need to sync with the system ntptime and notice how the fractional seconds stay (nearly) the same.|
|4||-t, –no-title||It will help to turn off the header showing the current time command, interval, at the top of the display.|
|5||-b, –beep||The beep is the command has a non-zero exit.|
|6||-e, –errexit||It will freeze the updates on command error and exit after an input or key press.|
|7||-g, –chgexit||It will exit when the output of command changes.|
|8||-c, –color||It will interpret the ANSI color and style sequences.|
|9||-x, –exec||The –exec option, which passes the command to exec (2) instead.|
|10||-h, –help||It will display the help information and exit it|
|11||-v, –version||It will print the version information and exit it|
Examples to Implement Linux Watch Command
In the Linux environment, it is a very easy and simple way to use the Watch command.
Example #1 – Linux Watch Command
As per the above command, we are trigging the date command with watch keyword. The command output window will appear as it is unless and until we do not kill it (via Ctrl+c option).
Example #2 – Change Time Interval
In watch command, we can the command execution time. We need to use the “-n” option with it.
watch -n 5 date
As per the above command, we are changing the command execution time in second. The date command will run every 5 seconds.
Example #3 – Command Difference
In watch command, we are having the functionality to get the difference in between the command output.
watch -d date
In watch command, we are able to find the command difference on the CLI window. The command difference will highlight by the watch command.
Example #4 – Omit Header Information
In watch command, we are able to remove the header information from the command output.
watch -t date
As per the above command, we are removing the header information of the watch command.
Example #5 – Auto Exit if Command Output Change
In watch command, we are having the functionality to exit the output window when the command output was changed.
watch -g date
In screenshot 1 (a), we are seeing the watch command output. But when command difference is observed then the screen exit automatically (refer screenshot 1 (b)).
Screenshot 1 (a)
Screenshot 1 (b)
We have seen the uncut concept of “Linux Watch Command” with the proper example, explanation, and command with different outputs. As per the requirement, we can change the command execution time in the watch command. It is very useful to observe the command difference.
This is a guide to Linux Watch Command. Here we discuss an overview of the Linux Watch Command and its different commands along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –