Updated June 20, 2023
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 command output 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 protected]); after that, it will modify and corrected by Francois Pinard. After the time interval, there are lots of changes happening in utility.
Syntax :
watch [ options ] command
- watch: We can use the “watch” keyword in the syntax or command. It will take different arguments like options and 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 different Linux commands.
How does the Linux Watch command work?
In the Linux environment, we can print the command in a specific time interval. But in the watch command, we are printing the command output rapidly and observing the command output difference if any.
In the Watch command, below are the different exit codes available.
- 0: Once the job or command is successfully executed, it will print the “0” exit code.
- 1: Once the job or command fails, it prints the “1” exit code.
- 2: when the job or command forking the process to watch the failed, it will print the “2” exit code.
- 3: When it replaces the child process of stdout with the write side pipe failed, it will print the “3” exit code.
- 4: When the command execution fails, 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 fails, it prints the “7” exit code.
- Other: It will watch the command or job and propagate the command exit status as the child exit status.
Below are the lists of options that are compatible with the watch command.
Sr No | Option | Description |
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 ensure that the watch attempts to run the command every interval of 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 and 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 the command changes. |
8 | -c, –color | It will interpret the ANSI color and style sequences. |
9 | -x, –exec | The –exec option 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
Command :
watch date
Explanation:
As per the above command, we are trigging the date command with the watch keyword. The command output window will appear as it is unless and until we do not kill it (via the Ctrl+c option).
Output :
Example #2 – Change Time Interval
In the watch command, we can see the command execution time. We need to use the “-n” option with it.
Command:
watch -n 5 date
Explanation:
As per the above command, we are changing the command execution time in seconds. The date command will run every 5 seconds.
Output:
Example #3 – Command Difference
In the watch command, we can get the difference between the command output.
Command:
watch -d date
Explanation:
In the watch command, we can find the command difference on the CLI window. The command difference will highlight by the watch command.
Output:
Example #4 – Omit Header Information
In the watch command, we can remove the header information from the command output.
Command:
watch -t date
Explanation:
As per the above command, we are removing the header information of the watch command.
Output:
Example #5 – Auto Exit if Command Output Change
In the watch command, we can exit the output window when the command output is changed.
Command:
watch -g date
Explanation:
In screenshot 1 (a), we see the watch command output. But when a command difference is observed, then the screen exit automatically (refer to screenshot 1 (b)).
Output:
Screenshot 1 (a)
Screenshot 1 (b)
Conclusion
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.
Recommended Article
We hope that this EDUCBA information on “Linux Watch Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.