In a previous job, I couldn't get a reliable watcher with just command-fu, so I had a wrapper script like the one below, which inspected the file every poll_duration seconds and grepped the new lines for the interested phrase.
#!/bin/bash
file=$1
phrase=$2
poll_duration=$3
typeset -i checked_linecount
typeset -i new_linecount
typeset -i new_lines
let checked_linecount=new_linecount=new_lines=0
echo "Watching file $file for phrase \"$phrase\" every $poll_duration seconds"
while [ 1 ]
do
let new_linecount=`wc -l $file| awk '{print $1}'`
if [[ $new_linecount > $checked_linecount ]]; then
let "new_lines = $new_linecount-$checked_linecount"
head --lines=$new_linecount "$file" | tail --lines=$new_lines | grep "$phrase" && beep
let checked_linecount=$new_linecount
fi
sleep $poll_duration
done
This was on a Unix machine. On Linux, you can go one better by using its inotify filewatcher interface. If this package (inotify-tools on Ubuntu) is present, replace
sleep $poll_duration
with
inotifywait -e modify "$file" 1>/dev/null 2>&1
This call blocks till the file is modified. The blocking version is almost as efficient as what you'd get with the tail -f
version if pipe could be configured to work without buffering.
Note: The script first does a head --lines=$new_linecount
to ensure that lines added to the file after we checked it do not skew the chunk of the file that gets checked in this loop.
there is a beep program with is in the default repo for debian and variants just apt-get install beep but it doesn't work with the piping this way – Jakob Cosoroaba – 2009-09-07T13:51:58.817