I am trying to monitor a log file and kick of a script when a change occurs. I know I could do this with a cron job and check for differences at set intervals, but I need the script to be dynamic and notify me as soon as there is a change in the log file. If anyone knows of a way to do this please help.
#! /bin/sh
# This is the log that will be monitored.
# If any changes occur to this, then take appropriate action.
MONITORED_LOG=/tmp/goreb.log
# We will need this log to see whether any changes occured to /tmp/goreb.log
TEMP_LOG=/tmp/.goreb.log.1
# This is a 1-time command i.e. create the log file if it does nto exist.
[ ! -f $TEMP_LOG ] && touch -r $MONITORED_LOG $TEMP_LOG
[ $MONITORED_LOG -nt $TEMP_LOG ] && echo "$MONITORED_LOG is newer than $TEMP_LOG. Carry out your required operations"
# Update $TEMP_LOG with the new modified date of $MONITORED_LOG
touch -r $MONITORED_LOG $TEMP_LOG