Ever have a time where you needed to continuously check the progress of something, for example the progress of your array rebuilding after replacing a hard drive? Or waiting for the status of something to change?
Well instead of running the same commands over and over again, there is an easier way. You can simply use the watch command to periodically run the specified command or script for you at whatever interval you would like. Below is the usage as-well as available options.
Watch Command Usage
watch [options] “command”
|-b||Beep if command has a non-zero exit (if command fails)|
|-c||Interpret ANSI color and style sequences|
|-d||Highlight changes between updates|
|-e||Exit if command has a non-zero exit|
|-g||Exit when output from command changes|
|-n||Interval <secs> seconds to wait between updates|
|-p||Attempt run command in precise intervals|
|-t||Turn off header (shows interval and command)|
|-x||Pass command to exec instead of “sh -c”|
|-h||Display this help and exit|
|-v||Output version information and exit|
Watch Command Examples
Here are a few examples of using the watch command. I will be using it to watch the output of “date” for the sake of this guide, since everyone running Linux should have the date command available. To terminate watch and whatever command or script it is currently running, you can simply use CTRL+C.
Refresh every 5 seconds:
watch -n5 "date"
Refresh every 5 seconds and highlight any output changes:
watch -n5 -d "date"
Refresh every 5 seconds and exit if output changes (you will not see the output if you choose this option):
watch -n5 -g "date"
I hope you liked this post about the watch command, please don’t forget to like, comment or share this post. Thanks!