I have the below bash script set to run every hour via cron as the root user. The purpose of this script is to email us when files have been uploaded to our sftp server and send us the logins. Here is the script
build_report() {
# This function builds the report header and gets the list of files
#Get all the files under /home/SFTP
local f=($( find "/home/SFTP" -type f | tr " " "_" ))
echo
echo "********************************************";
echo "*************** FILE REPORT ****************";
echo "********************************************";
echo "**** SEARCHING THROUGH SFTP FOLDERS ******";
echo "********************************************";
echo "* IF I FIND SOMETHING, I WILL LIST IT HERE *";
echo "********************************************";
echo "********************************************";
echo "GENERATED ON $TIMESTAMP ";
echo
echo
#Loop through all the files and list list them
for i in " ${f[@]}"
do
echo $i
done
}
sftp_log() {
#This function checks the /var/log/auth.log file for sessions
echo "*****************Begin Access Log*********************"
cat /var/log/auth.log|grep -E "interactive/pam"
}
TIMESTAMP=$(date)
files=$(find "/home/SFTP" -type f | tr " " "_")
#If there are files present create the report, email it and log we found something.
#Else, log we didn't find anything
if [ "$files" != "" ]; then
{ build_report && sftp_log; } | awk '{print $0,"\n"}' | mail -s "report" user@ourdomain.com
echo $TIMESTAMP " Files found. Email Sent">>filereport.log
else
echo $TIMESTAMP " No files found" >>filereport.log
fi
exit 0
The issue comes at 8 AM every day. Here is an example of what happens
7AM: Files are present. The report is sent correctly with the output of both functions.
8AM: Same files are present. The report is sent with only the output of the build_report function
9AM: Same files are present. The report is sent with only the output of the build_report function
10AM: Files still present (could be new files added, could be the same file). An email is sent with the output of both functions correctly and the login that occurred between 8-9 is now present.
I set the auth.log to rotate once a week to make sure there wasn't some kind of file rotation/file lock issue. Also, if there are files present at 7AM, we get a correct report then.
Any ideas? My bash scripting ability is crude at best, so any ideas are welcome