Skip to content

Instantly share code, notes, and snippets.

@titpetric
Last active December 19, 2015 16:38
Show Gist options
  • Save titpetric/5984831 to your computer and use it in GitHub Desktop.
Save titpetric/5984831 to your computer and use it in GitHub Desktop.
Incremental reading of files that get rotated. If files are being written to it ignores the bytes written since the first `stat` call, avoiding double reads. Fairly untested so far, not even sure if it recognizes that the log rotation was performed. Mileage may vary, feel free to submit fixes.
#!/bin/bash
function usage {
echo Usage: $0 logfile.log logfile.log.1 [name]
exit
}
if [ -z $1 ]; then
usage
fi
if [ -z $2 ]; then
usage
fi
LOGNAME=".offset"
if [ ! -z "$3" ]; then
LOGNAME=".$3"
fi
LOG_SIZE=`stat -c %s $1`
LOG_OFFSET=0
if [ -f "$1$LOGNAME" ]; then
LOG_OFFSET=`cat $1$LOGNAME`
else
# first run, skip all data so far
echo $LOG_SIZE > $1$LOGNAME
exit
fi
if (( $LOG_SIZE > $LOG_OFFSET )); then
CHANGED=`expr 1 + $LOG_SIZE - $LOG_OFFSET`
tail -c +$LOG_OFFSET $1 | head -c $CHANGED
else
LOG_SIZE2=`stat -c %s $2`
if (( $LOG_SIZE2 > $LOG_OFFSET )); then
tail -c +$LOG_OFFSET $2
fi
cat $1 | head -c $LOG_SIZE
fi
echo $LOG_SIZE > $1$LOGNAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment