DEV Community

Cover image for Linux Commands: watch
Umesh Yadav
Umesh Yadav

Posted on • Updated on • Originally published at umesh.dev

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.

Syntax

watch [options] command
Enter fullscreen mode Exit fullscreen mode

Let's see the watch command in the action. We will run watch uptime to check the uptime of the machine.

Screenshot 2020-11-02 at 12.26.58 AM.png

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 watch command.

  • -d, –differences: This option highlights the values which have changed between successive updates. You can also pass the -d=permanent option 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.
Screenshot 2020-11-02 at 12.37.40 AM.png

Let's try to run watch -d=permanent uptime. Now the values which are changed at least once stay highlighted throughout.
Screenshot 2020-11-02 at 12.39.05 AM.png

  • -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
Screenshot 2020-11-02 at 12.47.54 AM.png

  • -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.

Screenshot 2020-11-02 at 1.08.49 AM.png

  • -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.

Screenshot 2020-11-02 at 1.12.58 AM.png

Advance Usage

You can also watch the output of multiple commands.

$ watch "[command] | [command]"
Enter fullscreen mode Exit fullscreen mode

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.

Screenshot 2020-11-02 at 1.20.33 AM.png

Conclusion

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 watch.

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. Also please follow me here and on Twitter for future blogs which I write over here

Top comments (0)