How to write a unix shell script that polls for files at a s

March 23, 2012 at 01:43:39
Specs: Windows XP
I need to write a shell script that polls for files in a directory at 9 am evry 2 minutes,once the files are found,then trigger the next job.

See More: How to write a unix shell script that polls for files at a s

Report •

#1
March 23, 2012 at 10:50:03
One way is to use a script that checks for the existence of the file and does something if it finds it. If it doesn't find it, wait for 120 seconds and try it again. Since this problem will run from cron, I would not let it run forever. I let it run for 40 minutes, but choose whatever suits you:
#!/bin/ksh

# Not TESTED from cron
mydir="/tmp/"
count=20

cnt=0
while true
do
   ((cnt+=1)) # bump the counter by 1
   # do something if the file exists
   if [[ -f $mydir/thefile.txt ]]
   then
      echo "Do something"
      exit
   fi
   if [[ $cnt -gt $count ]]
   then # stop the loop if file does not appear
      echo "waited too long.  stopping"
      exit
   fi
   sleep 120
done
# end script

Set your job to run from cron at 9:00 AM. If you are not familiar with cron, check out this link:

http://www.linuxweblog.com/crotab-t...

Let me know if you have any questions.


Report •
Related Solutions


Ask Question