/home/mrowcp/ CREATE filefile Wed Mar 7
/home/mrowcp/ CREATE filefile123 Wed Mar 7
/home/mrowcp/ CREATE newfilecreate.txt Wed Mar 7
Now the problem is in mail sending.
How Can I send each of this new created files?May be I need another script for inotify.log monitoring, and if new line is created:
I would suggest a cron job that run every N mins and checks for any new entries in the inotify.log file. I will need to "remember" where in the log it has already processed and I'd suggest a dat file like /home/mrowcp/.inotify.dat.
The script can then find any new entries from after this line:
CHKFILE=/home/mrowcp/.inotify.dat
LOGFILE=/var/log/inotify.log
NOTIFY=mrowcp@domain.com
show_details()
{
echo " New file(s) have arrived: "
echo "$newfiles" | sed 's/^/ /'
}
# Last line to looked at - just in case a file arrives while we are working
EL=$(wc -l $LOGFILE | awk ' { print $1 }')
if [ -f $CHKFILE ]
then
SL="$(cat $CHKFILE)"
# Old position is greater than size of file - It's may have been truncated so start from begining
[ $EL -lt $SL ] && SL=0
else
# No remembered previous line - start from begining
SL=0
fi
newfiles=$(awk -vS=$SL -vE=$EL '
NR>E{exit}
/^\/home\/mrowcp\/ CREATE/&&NR>=S{print $3}' $LOGFILE)
if [ -n "$newfiles" ]
then
show_details | mail -s "new FTP files" $NOTIFY
fi
echo "$EL" > $CHKFILE
Looks like the .inotify.dat file is getting into the log file so probably best to just filter that one out.
You could also put the .inotify.dat file in another directory as the awk code only looks for files in the /home/mrowcp/ directory
CHKFILE=/home/mrowcp/.inotify.dat
LOGFILE=/var/log/inotify.log
NOTIFY=mrowcp@domain.com
show_details()
{
echo " New file(s) have arrived: "
echo "$newfiles" | sed 's/^/ /'
}
# Last line to looked at - just in case a file arrives while we are working
EL=$(wc -l $LOGFILE | awk ' { print $1 }')
if [ -f $CHKFILE ]
then
SL="$(cat $CHKFILE)"
# Old position is greater than size of file - It's may have been truncated so start from begining
[ $EL -lt $SL ] && SL=0
else
# No remembered previous line - start from begining
SL=0
fi
newfiles=$(awk -vS=$SL -vE=$EL '
NR>E{exit}
/^\/home\/mrowcp\/ CREATE/&&NR>=S&&$3!=".inotify.dat"{print $3}' $LOGFILE)
if [ -n "$newfiles" ]
then
show_details | mail -s "new FTP files" $NOTIFY
fi
echo "$EL" > $CHKFILE