Tuesday·30·August·2011
Useful but Unknown Unix Tools: watch //at 22:18 //by abe
Yet another useful tool of which at least I heard quite late in my Unix career is “watch”. For a long time I wrote one-liners like this to monitor the output of a command:
while :; do echo -n "`date` "; host bla nameserver; sleep 2; done
But it’s way shorter and less error-prone to use “watch” from Debian’s procps package and just write
watch host bla nameserver
The only relevant difference is that I don’t have some kind of history when the output of the command changed, e.g. to calculate the rate with which a file grows.
You can even track the output of more than one command:
watch 'ps aux | grep resize2fs; df -hl'
Also a nice way to use watch is to run it inside GNU Screen (or tmux or splitvt) and split up the terminal horizontally, i.e. show the output of watch in one window and the process you’re tracking with the commands run by watch in the other window and see both running at the same time.
Update, Sunday, 28th of August 2011, 17:13h
I never found a useful case for watch’s -d
option which
highlights changes to the previous run (by inverting the changed
bytes), but until now three people pointed out the -d
option in response to this blog-posting and weasel also had some nice
examples, so here are they:
Keep an eye on the current network routes (once per second) of a host and quickly notice when they change:
watch -n1 -d ip r
Watch the current directory for size or time stamp changes of its files:
watch -d ls -l
The option -d
only highlights changes from the previous run to the
next run. If you want to see all bytes which ever changed since the
first run, use --differences=cumulative
.
Thanks to Klaus “Mowgli” Ethgen, Ulrich “mru” Dangel, Uli “youam”
Martens and Peter “weasel” Palfrader for comments and suggestions.
Tagged as: GNU Screen, nuggets, procps, screen, splitvt, tmux, UUUT, watch
// write a comment // comments off