Link to home
Start Free TrialLog in
Avatar of ITApprendista
ITApprendista

asked on

Script to Monitor a Directory in Linux

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.




ASKER CERTIFIED SOLUTION
Avatar of supermandaddy
supermandaddy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of woolmilkporc
#!/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
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

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
@wmp - I agree. Btw: your suggestiom has an 'exit 99', I don't think that was asked for :)
@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