Watch – A Useful Linux Command You May Have Never Heard Of
beerpla.net
beerpla.net
For this sort of thing I use a simple perl script called atchange. I could do:
atchange . ls
And I'd see list output whenever a new file appeared in the directory. The hacky thing I usually use it for: atchange my_source_file make
to make things rebuild when I save my_source_file in my editor.The script is here: http://www.ccrnp.ncifcrf.gov/~toms/ftp/atchange The webpage about it is here: http://www-lmmb.ncifcrf.gov/~toms/atchange.html
I first learnt about it years ago while reading the entertaining ``Work'' Columns by Jeffreys Copeland & Haemer: http://alumnus.caltech.edu/~copeland/work/
I'm not sure if my ego is being stoked or bruised, but it irks me every time I see a post like "10 Commands You've Never Heard Of"... and it's either a list of esoteric commands used by low level coders or commands that have fallen out of favor over the years.
And I'm familiar with every one of them. I've seen this round of knowledge cycle full circle a couple times now. I can't imagine the eye rolling of neckbeards who've been around professionally since the 80s.
It only works if you're either exceptionally clever, or are passing out some original information.
MSNBC (and by extension, their advertisers) has really been taking advantage of it by drawing readers to their many recent "top ten" lists.
The latter can be extremely informative and provide a good starting point for someone. The former is filler.
I was wondering what the hell was going on with comments on that page today, before looking at the stats: http://i.imgur.com/tVhwK.png
watch 'ls -l | tac'
which reverses the lines of the output, or if it is something like a log file, you can forget about watch completely and just use tail -f, e.g. tail -f logfile.log
which will print new lines as they are added to the file.Fast-forwarding to now, having just installed inotify-tools (Debian/Ubuntu package):
while true; do inotifywait . &> /dev/null ; ls ; done
is a much more responsive version of: atchange . ls
I should probably make a wrapper that uses inotify when available and degrades to atchange's brute force approach when necessary. while true; do inotifywait -qq -e modify [filename]; [command]; done
or as a shell function, defined in .bashrc to have the same form as the atchange perl script: function atchange(){
while true;
do inotifywait -qq -e modify $1;
$2;
done if which inotifywait > /dev/null ; then
function atchange(){
FILE="$1"
shift
while inotifywait -qq -e modify -e attrib "$FILE"; do
"$@";
done;}
fi
Looking again at atchange it deliberately waits after first noticing the file has been modified, in case something is still modifying it. It fires once writing has stopped for at least a second. I'm not sure if I need that in my use cases, but its authors clearly did so I'm pointing out that the above shell function does not provide a true like-for-like replacement. (Also atchange allows a single argument to be a command_file, functionality I never use.)EDIT changed the "while true" so that the function stops on an error rather than infinitely looping when something goes wrong, like FILE ceasing to exist. A file temporarily disappearing would scupper the script though, whereas the perl script deals quietly with that.
watch -n 1 "ps aux | grep something"
watch -n 1 "killall some-runaway-process"
etc
diff the-article the-man-page | wc -w
=> 75I guess we just disagree about what part of the post is useful.
I looked around and this is all I could find: http://www.sveinbjorn.org/watch_macosx
I have a bunch of while loops written in bash, but would prefer something cleaner.
watch -n 1 ps -eo pid,ppid,wchan=WIDE-WCHAN-COLUMN,stat,command r"watch" does not make the computer watch anything (at least in its default mode of operation); it makes the computer run a command repeatedly and the user watches it. "repeat" or "refresh" might have been more consistent names.
*every 2s ls*
*echo "ls" | every 5m
For readability, I would then drop the default for the time interval; the command without the time interval (every ls) reads awfully.And yes, that first way of specifying what to repeat is different from the way at does it. I am not sure I would want to follow 'at' in that respect.
http://www.linuxquestions.org/questions/*bsd-17/linux-watch-... gnu-watch, cmdwatch, gnuwatch etc.
watch -n .2 <cmd>
and it'll go every .2 secondsIIRC there's an example in _The Awk Programming Language_ implementing a toy make out of a topological sorter. (Edit: pgs. 170-8.)
EDIT: Topological sorting is one of those tools that really comes in handy -- it's easy enough to implement, but I didn't want to spend the time either (a) writing it in zsh or (b) rewriting the rest of the shell pipeline in Python or something. 'tsort' to the rescue! I just wish I could remember where I read about it so I could go buy whoever was at the other end of the Google search a beer.
watch -n 1 -d 'echo "SHOW PROCESSLIST;" | mysql' while 1; do datetime; ls; sleep 2; done while true; do date; ls; sleep 2; clear; done
but got tired of it and searched apt for watch. apt-get install procps while true; do date; ls; sleep 2; clear; done
but got tired of that and installed procps via apt.watch -d sudo netstat -tnp
while true; do ls; sleep 5; done
When I want to monitor a directory, I let the kernel tell me via inotify.