Linux Commands: watch
watch is used to run a command repeatedly and display its output and error at regular intervals. This is useful when you want to run a command repeatedly and observe its output getting changed over a period of time. By default, the command is run every 2 seconds and the watch will run until interrupted(Ctrl+c).
watch command can be used to check the status of service, the uptime of machine, disk usage, etc.
watch [options] command
Let's see the
watch command in the action. We will run
watch uptime to check the uptime of the machine.
As you can see in the above image after you run the
watch command it will start running in the fullscreen mode. On the top left corner, you can see the refresh interval(
Every 2.0s: uptime) which is two seconds as we haven't specified any interval, it takes the default one.
Let's see what all options we can provide to the
- -d, –differences: This option highlights the values which have changed between successive updates. You can also pass the
-d=permanentoption to make the changes highlighted even if they have changed at least once.
We will just run
watch -d uptime. The output looks something like below.
Let's try to run
watch -d=permanent uptime. Now the values which are changed at least once stay highlighted throughout.
- -n, –interval seconds: This option specifies the refresh interval. It can't be quicker than 0.1 seconds.
We can change the refresh interval using
watch -n 1 uptime
- -p, --precise: This option makes sure the update fractional seconds stays (nearly) the same, as opposed to normal mode.
We can check this command by running
watch -p date +"%T.%s". Observe the time with and without the precision option you should see that with the precision flag the time difference is pretty much the same for each refresh while that's not true for normal mode.
- -t, –no-title: This option is used to turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.
- -b, –beep: This option will give beep if the command has a non-zero exit.
- -e, --errexit: This option freeze updates on command error, and exit after a keypress.
- -g, --chgexit: This option exit when the output of the command changes.
- -h, --help: Display help text and exit.
- -v, --version: Display version information and exit.
You can also
watch the output of multiple commands.
$ watch "[command] | [command]"
For example, the following command will print all the node process running on my machine. It uses ps to output the process and then utilizes
grep to filter the node process.
watch is a very powerful Linux command which you can use to monitor the output of anything you want to do repeatedly. I have used
watch to check daemon status while deploying service in production and while debugging crashing services in production. Do let me know how you are using the
If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter @imumesh18. You can subscribe to my newsletter to read more from me.