Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 down vote accepted

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

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

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

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

Your Answer


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.