Script to Monitor a Directory in Linux

ITApprendista
ITApprendista used Ask the Experts™
on
Hello,

I am looking to write a script on a Redhat system that will watch a folder for activity.  If no new files are created after 10 minutes it will do something (haven't decided what yet - email someone, send an alert of some kind)

Essentially there is a nightly process that kicks off at a certain time each day.  While it is running it is writing files to this directory indicating that various parts of the process have completed.  When it's finished it creates a file called End.txt signaling that it completed.  

So, the script I need would run until it sees that End.txt exists.  While it is running it would check for the timestamp of the newest file and compare it to the current time.  If the file is older than 10 minutes it would send the alert.  It would stop running once the End.txt is located.




Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MAILBOX=some_directory
seq=0
while true; do
   let seq=seq+1   # this might be bash specific
   if  /bin/ls -1 $MAILBOX | grep ^ ; then
      # make sure incoming files aren't mixed up with files-to-be-processed
      mv $MAILBOX $MAILBOX.$seq
      mkdir $MAILBOX
      # your function
      sftp_this_directory $MAILBOX.$seq
      # clean up
      rm -rf $MAILBOX.$seq
   fi

   sleep 300 # wait 5 minutes
done
Most Valuable Expert 2013
Top Expert 2013

Commented:
#!/bin/sh
cd /path/to/folder
while [ ! -e End.txt ] ; do
if [ $(($(date +%s)-$(stat -c %X $(ls -rtd * |tail -1)))) -gt 600 ]
 then echo "Newest File too old!"
      exit 99
fi
sleep 5
done
echo "End.txt found!"
exit 0
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Hi,

See script below. First assume recent files exist. Script will stop when End.txt is found and if no more recent files (10 minutes) are found. If you want it to exit immediate, a second check for End.txt can be put before the sleep command to break the loop immediately.

YP=/your/path/to/monitor
YF=End.txt
# assume recent files exist in your path (YP)
RF=1
while [ ! -f ${YP}/${YF} ] && [ "${RF}" == "1" ]
do
  if [ `find 2>/dev/null ${YP} -mmin -10 -exec ls -l {} \; | wc -l` -gt 0 ]
  then
    # recent files exist (or exist again)
    RF=1
    echo "Recent file(s) found."
    sleep 10
  else
    RF=0
    echo "No (more) recent file(s) found - trigger your alarm action!"
  fi;
done;
echo "${YF} file found, exiting."

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Most Valuable Expert 2013
Top Expert 2013

Commented:
I can't see at all how the accepted suggestion could solve your issue.

It rather seems that it had accidentally been posted in the wrong thread!

Could you explain why you accepted it nonetheless?

wmp
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
@wmp - I agree. Btw: your suggestiom has an 'exit 99', I don't think that was asked for :)
Most Valuable Expert 2013
Top Expert 2013

Commented:
@gerwinjansen - Thanks for the confirmation!

As for the "exit": Exiting there gets me rid of the need to handle the "record found" condition - what you solved with the "$RF" thing.

And "exit 99" - well, the asker wrote "If no new files are created after 10 minutes it will do something (haven't decided what yet)", so what should I have suggested?
Any desired action could be placed before "exit", and as long as it's not there I wanted at least to provide a returncode.

But anyway, while our solutions will more or less do what was asked for I still wonder what the accepted solution should have contributed.

Could it be worth the effort requesting a moderator's attention here? Don't really think so.

Cheers

wmp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial