Commands you might have missed: watch

Posted by Steve on Wed 16 Jul 2008 at 09:30

There are many times when it is useful to be able to repeatedly run a command, or set of commands, repeatedly. You could do this yourself with a simple shell script, but using watch makes it simple.

In brief watch is a command which will repeatedly run a command for you - allowing you to watch updates in near real-time.

The most basic example of using it would be to run this:

skx@teldra:~$  watch uptime

If you execute the above you'll find that you've got a window updating with the output of the uptime command every few seconds.

There are many times when this is a useful thing to do, such as keeping track of a rebuilding RAID array:

skx@teldra:~$  watch cat /proc/mdstat

The watch command does accept several command line arguments, the most interesting of which are:

-n

Specify the number of seconds to wait between re-running the command. e.g. "-n 5"

-d

Highlight differences each time the output changes.

This is something that I often run to see the most recent files to be uploaded in my incoming area:

skx@teldra:~$  watch -n 5 "ls -ltr | tail -n 20"

The watch command is contained in procps package and may be installed via "apt-get install procps" if it isn't already available.


This article can be found online at the Debian Administration website at the following bookmarkable URL (along with associated comments):

This article is copyright 2008 Steve - please ask for permission to republish or translate.