Search

watch (from procps): execute a program at regular intervals, and show the output

December 21st, 2008 edited by Vicho

Article submitted by Kris Marsh. If you celebrate Christmas, you can give to Debian Package of the Day a nice present: a good article! :-)

Ever wanted to monitor a directory every second and see differences in filesizes per second? Or for that matter, run any program once a second and highlight differences in time? Well you can, and you have been able to since forever as it’s installed by default on the majority of Linux distributions. watch is part of the procps package, available in Debian and Ubuntu.

Here is an example for checking a directory:

watch ls -l

To highlight changes in each program run, you can use the -d flag:

watch -d ls -l

And to run the command every N seconds, use -nN (by default, watch runs every 2 seconds):

watch -n1 -d ls -l

Finally, to make the diff highlighting “sticky” (i.e. stay on permanently after a change is detected), use: -d=cumulative

Other examples:

  • Watch your log directory for changes
    watch -d=cumulative -n1 ls -lt /var/log
  • Watch for new email
    watch -n60 from
  • Monitor free memory
    watch -n10 free -m
  • Monitor established connections
    watch -n1 -d 'netstat -an | grep ESTABLISHED'

… you get the point. If you’re a system administrator, or just maintain Linux machines in general you’ll probably spot a bunch of places where you can use this straight away.

Posted in Debian, Ubuntu |

6 Responses

  1. George Gesslein II Says:

    For those of you who don’t have the watch command installed, this extremely simple shell script works pretty much the same:

    #!/bin/bash
    # Watch output from a shell command, by executing it every one second.
    if [ "$1" == "" ]
    then
    echo Usage: watch command
    exit
    fi
    clear
    while ( bash -c “$*” )
    do
    sleep 1 && clear
    done

  2. Vicho Says:

    George Gesslein II said:

    “For those of you who don’t have the watch command installed”

    procps has “Priority: Required”, so it should be installed in every Debian or Ubuntu system. Therefore, watch is always available.

  3. zohair Says:

    i find that while writing iptable rules, its useful to do a ‘watch iptables -l -v’ in terminal window while writing them. Of course that assumes your rules fit in one terminal window :P

  4. Roshan Says:

    God, this is brilliant. It’s exactly what I wanted. I’ve been using a sleep in a for loop all this while. Wonderful!

  5. MarkC Says:

    I use this from time to time if I’m using “dd” to copy lots of data. You can send a SIGUSR1 to dd to get it to display the I/O stats to date (see the dd man page) - doing this using “watch” is a handy way to keep track on the progress of your transfer.

  6. Daniel Hahler Says:

    I also like this example from the man page:

    You can watch for your administrator to install the latest kernel with
    watch uname -r