Script to find directory is getting files in every 10 mins, if not then when last time file received

Dears,

I am looking for a script which will work as a watch directory.
I ha directory which keep getting files in every 10 mins and some time delay.
I want to monitor if the directory getting the files in every 10 mins if not captured the last received file time and calculate the delay.

Regards,

So every 10 minutes, check the directory and if the most recent file is greater than 10 minutes old send a mail with the subject "No new files seen since ${LATEST_FILE_TIMESTAMP}"

You can use cron to run the check every 10 minutes.

and the rest could be acomplished with something like the following:

if [ $(( $(date +%s ) - $(stat -t  $(ls -lrt ${PATH_TO_RECEIVING_DIRECTORY}| tail -1 | awk '{print $NF}') | awk '{print $13}')  )) - gt 600 ]; then #
  mailx -S "No NEW FILES SEEN SINCE $(date --date=@$(stat -t  $(ls -lrt ${PATH_TO_RECEIVING_DIRECTORY}| tail -1 | awk '{print $NF}') | awk '{print $13}' )" sadique.manzar@unix.com
fi 
1 Like

Throws an error.line 5: unexpected EOF while looking for matching `"

Hi Sadique,

I left out a closing parenthesis, I've also added full path to the stat command:

if [ $(( $(date +%s ) - $(stat -t  ${PATH_TO_EXISTING_DIRECTORY}/$(ls -lrt ${PATH_TO_RECEIVING_DIRECTORY}| tail -1 | awk '{print $NF}') | awk '{print $13}')  )) -gt 600 ] ; then
   echo  mailx -S "No NEW FILES SEEN SINCE $(date --date=@$(stat -t  ${PATH_TO_EXISTING_DIRECTORY}/$(ls -lrt ${PATH_TO_RECEIVING_DIRECTORY}| tail -1 | awk '{print $NF}') | awk '{print $13}' ))" sadique.manzar@unix.com
fi

Still an error:

line 3: 1534159198 - : syntax error: operand expected (error token is "- ")

Which shell are you using?

I'm relying on a Bash convention here that anything inside $(( ...)) gets mathematically evaluated in the condidtion.

Why not check the modified date of the directory itself? With the assumption that nothing else is going on there (any maintenance to be considered separately), it should represent the last file's arrival. Try

cd ${PATH_TO_RECEIVING_DIRECTORY}
 if (( ( $(date +%s) - $(stat -c%Y .) ) > 600 )); then echo "overdue file"; else echo "file in time"; fi
cd BACK_TO_WHERE_YOU_CAME_FROM

 
1 Like