Friday, October 23, 2009

Real time log file watcher

Have you ever wanted to be notified - in real-time - of an event in a log file?

This is a very efficient and effective way to do it.. all you need is inotifywait (in inotify-tools package, and of course inotify enabled kernel which is pretty standard these days).

#!/bin/bash
LOGFILE=/var/log/smdr.log
while inotifywait -qq -e modify $LOGFILE; do
x=`tail -n1 $LOGFILE | grep "something"`
if [ "$x" != "" ]; then
  echo $x | mailx -s "Something happened!" you@abc.org
fi
done

No comments:

Post a Comment