This site is now 100% read-only, and retired.

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.

 

 


Re: Commands you might have missed: watch
Posted by Anonymous (89.16.xx.xx) on Wed 16 Jul 2008 at 09:49
Thanks. I've really missed this useful command!

[ Parent ]

Re: Commands you might have missed: watch
Posted by tweek (192.38.xx.xx) on Wed 16 Jul 2008 at 10:45
Heh… :-)
That reminds me of all the times, where I continually have been hitting <up> <enter>…
Thanks!

[ Parent ]

Re: Commands you might have missed: watch
Posted by Steve (80.68.xx.xx) on Wed 16 Jul 2008 at 11:03
[ View Weblogs ]

Even now I find myself writing little loops at times:

steve@home:~$ while true; do uptime; sleep 5; done
 10:29:27 up  1:00,  5 users,  load average: 0.05, 0.09, 0.02
 10:29:32 up  1:00,  5 users,  load average: 0.04, 0.09, 0.02
 10:29:37 up  1:00,  5 users,  load average: 0.04, 0.09, 0.02
 10:29:42 up  1:00,  5 users,  load average: 0.04, 0.08, 0.02
...

Steve

[ Parent ]

Re: Commands you might have missed: watch
Posted by Anonymous (212.2.xx.xx) on Wed 16 Jul 2008 at 11:37
while sleep 5 ; do uptime ; done

[ Parent ]

Re: Commands you might have missed: watch
Posted by Anonymous (59.176.xx.xx) on Thu 17 Jul 2008 at 12:43
Oooh. Pure unix elegance. Or unix golf.

Explaining it in words: while runs the sleep 5 thing, which exits (like true) with a 0 (the healthy return status), and therefore does the do uptime loop thing, ending up back at the start, ready for another run.

PJ

[ Parent ]

Re: Commands you might have missed: watch
Posted by Anonymous (91.17.xx.xx) on Wed 16 Jul 2008 at 11:08
Nice, I'm using watch quite often, but missed the "-d" flag so far. Learned yet another handy trick :)

[ Parent ]

Re: Commands you might have missed: watch
Posted by eric (82.250.xx.xx) on Wed 16 Jul 2008 at 18:24
[ View Weblogs ]
Really useful when trying to debug things like network connections. I used --interval=X but it seems that -n X is the same.
watch --interval=1 'netstat -tpan|grep openvpn'
I remember that I discovered this command in a linux formation...

:eric:
http://blog.sietch-tabr.com

[ Parent ]

Re: Commands you might have missed: watch
Posted by Anonymous (24.126.xx.xx) on Fri 18 Jul 2008 at 17:04
"netstat -tpanc | grep openvpn" does the same thing, the -c is for continuous. Assuming you're using GNU linux and not *nix

[ Parent ]

Re: Commands you might have missed: watch
Posted by Anonymous (211.27.xx.xx) on Fri 18 Jul 2008 at 02:11
thanks for this series 'commands you may have missed' - this one i had indeed missed.... after only about 10 years of admin-ing LOL.

[ Parent ]