Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a log file that occasionally has critical errors. Each time a critical error occurs, I'd like to have an email sent to myself notifying me of the change. Is there a unix tool / set of unix tools I can adapt for this purpose?

What I thought of doing was

tail -f logfile | grep CRITICAL > critical.errors
watch tail critical.errors   # And somehow, email on changes. 

However, the watch command doesn't take an action. So ... is there something like 'watch' that does. Or perhaps a good existing script that will do this? I can homebrew a bash/python solution, but I'd rather reuse existing tools for cleaner error handling, etc.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Try IWatch - it's a perl script based on inotify that does just what you need:

http://sourceforge.net/projects/iwatch/

You can also roll your own with inotify-tools, in particular inotifywait. Modifying example #2 from that page (untested):

#!/bin/sh
file=logfile
cp $file $file.last
while true; do
  EVENT=$(inotifywait --format '%e' $file)
  [ $? != 0 ] && exit
  [ "$EVENT" = "MODIFY" ] && ( diff $file $file.last | mail ... ) && cp $file $file.last
done

You could also keep a byte/line counter and only mail the new bytes/lines, but using diff is simpler. It could cause performance issues if the file gets big and changes constantly though.

share|improve this answer
    
Is there something like this for Windows? –  Mark Richman Nov 25 '13 at 14:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.