How To Monitor Linux / BSD System Over Time Without Scrolling Output Using watch Command

Posted on in Categories Linux, UNIX last updated June 25, 2017

watch-cmmand-on-linux-unix
YYou can use the watch command to execute a program or shell script periodically, display its output on screen repeatedly. That allows you to look at the program output change over time. By default, the program runs every 2 seconds. Aforementioned is useful to monitor memory utilization, RAID rebuilds progress or disk space usage over time without having to look at scrolling output.

Say hello to watch command

The watch command execute a program periodically, showing output in fullscreen mode. It runs command repeatedly, displaying its output. You can install gnuwatch under OpenBSD or cmdwatch under FreeBSD (see the comments for more information) to get the same result.

Installation

The watch command is installed by default all on Linux distros.

A note about FreeBSD

Install and use the cmdwatch command. It watches the output from a command at specified intervals. Install it using the pkg command.
$ sudo pkg install cmdwatch
OR
# cd /usr/ports/sysutils/cmdwatch/ && make install clean

A note about OpenBSD

Type the following command to install gnuwatch using pkg_add command.
$ doas pkg_add gnuwatch

A note about macOS (OS X)

Type the following command to install watch using brew command.
$ brew install watch

watch command examples

To watch memory usage, enter:
watch free -m
To run free -m program every 5 seconds, enter:
watch -n 5 free -m
To watch disk space usage:
watch df -h
To watch the contents of a directory change, you could use
watch -d ls -l
If you are only interested in files owned by user tom, you might run:
$ watch -d 'ls -l | fgrep tom'
To watch your Linux software RAID build progress, run:
$ watch cat /proc/mdstat

Where,

  • The -d flag will highlight the differences between successive updates.
  • The -n {SECOND} flag use to specify a different interval.

To see all supported options, run:
$ man watch
OR
$ watch --help
Sample outputs:

Usage:
 watch [options] command
 
Options:
  -b, --beep             beep if command has a non-zero exit
  -c, --color            interpret ANSI color and style sequences
  -d, --differences[=<permanent>]
                         highlight changes between updates
  -e, --errexit          exit if command has a non-zero exit
  -g, --chgexit          exit when output from command changes
  -n, --interval <secs>  seconds to wait between updates
  -p, --precise          attempt run command in precise intervals
  -t, --no-title         turn off header
  -x, --exec             pass command to exec instead of "sh -c"
 
 -h, --help     display this help and exit
 -v, --version  output version information and exit
 
For more details see watch(1).

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

3 comment

  1. For FreeBSD:

    cd /usr/ports/sysutils/cmdwatch && make install clean

    then substitute cmdwatch for watch as listed above.

    The other BSD’s should be very similar.
    The stock BSD watch is for snooping on other tty’s (which can be rather useful too 😀 ).

    But thx for this, it’s helpful to keep an eye on httpd errors, during web development, as well as busloads of other tasks.

Leave a Comment