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.
[ Parent | Reply to this comment ]
That reminds me of all the times, where I continually have been hitting <up> <enter>…
Thanks!
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | 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 ...
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
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 | Reply to this comment ]
[ Parent | Reply to this comment ]
watch --interval=1 'netstat -tpan|grep openvpn'I remember that I discovered this command in a linux formation...
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]