In UNIX/Linux, “
watch” is a resourceful utility for monitoring updates in the specified command output (including errors) by refreshing the results every 2 seconds until it is interrupted using the “Ctrl+c” shortcut key.
It makes it easier for you to monitor the updates in background processes, disk usage, system uptime, tracking errors, and many more.
In this article, you will learn different ways to use the watch command, with practical examples.
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Discussed Tools in this Article|
Syntax of the Watch Command
The watch command takes two arguments: one is the option, and the other is the command (used as an argument to the watch command).
$ watch [OPTION] [COMMAND]
Monitoring the Command Output Changes
Monitor the output changes for a specific command (with or without flags) by specifying it as an argument to the watch command.
$ watch date
The above command will monitor the changes in the date command by updating the output every 2 seconds, as shown.
Setting a Custom Interval for Command Output Changes
Use the “
-n” flag to set a custom interval to update the command output used as an argument to the watch command.
$ watch -n 1 date
The above command will execute the date command every 1 second.
Highlighting the Difference in the Command Output Changes
-d” flag will come in handy when multiple parameters are changing in the command output by highlighting each one of them when they change, as shown.
$ watch -d date
Or, pass “
cumulative” as a value to the “
-d” flag if you want all the values that have ever changed to stay highlighted.
$ watch -d=cumulative date
Turning Off the Header
-t” flag will remove the header from the watch command that contains the interval time, user-defined command, and current system time.
$ watch -t date
Alerting When the Command Fails
-b” flag will beep when the specified command used as an argument to the watch command returns an error or a non-zero exit status.
Note: It requires the beep package, which can be easily installed using “
sudo apt install beep” if it is not present.
$ watch -b date
Freezing When the Command Fails
-e” flag will freeze the monitoring when the specified command used as an argument to the watch command fails or gets the non-zero exit status, asking you to press a key to exit.
In the following example, I’ve created a file with the name “file.txt” that will be monitored using the watch and ls commands; when I remove that file, the monitoring will stop and ask for a key to press for quitting.
$ watch -e ls -l file.txt
Quit Monitoring When Command Output Changes
-g” flag will quit monitoring for the specified command used as an argument to watch command on its first output change.
$ watch -g date
Monitoring the Multiple Piped Commands
If you want to monitor multiple commands piped together, enclose them in single or double quotes; otherwise, only the first command will get executed.
$ watch "ls -l | tail -n10; date"
The watch command has eight exit statuses:
- 0: Success.
- 1: Various Failures.
- 2: Forking the process to watch failed.
- 3: Replacing child process stdout with a write side pipe failed.
- 4: Command execution failed.
- 5: Closing child process write pipe failed.
- 7: IPC pipe creation failed.
- 8: The specified command exited on an error.
That’s all an informed Linux user should know about the watch command.
If you have any issue or question regarding this topic, then feel free to ask it in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.