Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Created December 15, 2015 16:21
Show Gist options
  • Save EdgeCaseBerg/933233f255adc52821ad to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/933233f255adc52821ad to your computer and use it in GitHub Desktop.
Log Monitor
#!/bin/sh
if [ $# -eq 0 ]; then
echo "Usage: ./monitor.sh /my/log/file.log"
fi
if [ $# -eq 1 ]; then
LOGFILE=$1
fi
echo "Monitoring $LOGFILE"
AFILE="/tmp/monitor.log.a"
BFILE="/tmp/monitor.log.b"
KEEPDIFFING=1
SLEEPTIME=2
echo "Please wait, setting up monitoring"
tail $LOGFILE > $AFILE
sleep 1
tail $LOGFILE > $BFILE
echo "Monitor files ready"
echo "Checking logfile differences every second, will stop script once no changes are detected"
echo -n "Start at: " && date
while [[ $KEEPDIFFING -eq 1 ]]; do
DIFFCMD=`diff -q $AFILE $BFILE`
if [[ "Files $AFILE and $BFILE differ" == "$DIFFCMD" ]]; then
echo -n "." #Progress indicator that the script is running.
KEEPDIFFING=1;
tail $LOGFILE > $AFILE
sleep $SLEEPTIME;
tail $LOGFILE > $BFILE
else
echo "No update to log file in last $SLEEPTIME second(s), stopping script"
echo -n "Stopped at: "
date
KEEPDIFFING=0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment