Created
December 15, 2015 16:21
-
-
Save EdgeCaseBerg/933233f255adc52821ad to your computer and use it in GitHub Desktop.
Log Monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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