Need help with script for check for a file that has changed in the last minute
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Need help with script for check for a file that has changed in the last minute
Ok.. so here's my criteria:
if file hasn't been modified in the last minute then
while file hasn't been modified in the last minute then
sleep for 59 seconds
done
else
copy the file to another directory
fi
Would this simply be:
find /tmp/file -mmin -1 >/dev/null
if [ $? -eq 0 ]; then
while [ find /tmp/file -mmin -1 >/dev/null -eq 0 ]; do
sleep 59
done
else
cp /tmp/file /somewhere
fi
Or am I way off track, or looking at this the wrong way?
Last edited by EclipseAgent; 02-11-2009 at 07:49 PM.
How would I then feed that into the script and taking that output that the file was written in the last minute?
Here's what I have going on:
the script calls a wrapper perl script and the parent process is killed
now I have to wait and keep searching for that file to be written
i was using find because the file name would be something like file_2009_02_11_20_33.xml (YYYY_MM_DD_HH_mm). However, more then 2 of these can run in a day (if someone does manual interaction SO I just look for a file of that name (YYYY_MM_DD*) that was written in the last minute, and if nothing then sleep for 59 seconds. (might switch to 58).
Now with stat how would I then take the output of that and compare it to changes in the last minute? Would I just:
stat -c %y /tmp/file | awk {'print $2'} and compare to date? (i'd have to check awk to see how to take out the .0000000000)
intvl=$(($(date +%s) - $(stat -c %Y /tmp/file)))
while [ $intvl -gt 60 ]; do
echo "I feel like sleeping"
sleep 5
intvl=$(($(date +%s) - $(stat -c %Y /tmp/file)))
done
echo "I worked for once"
Would that skew the result of:
intvl=$(($(date +%s) - $(stat -c %Y File_`date +%F`*.xml)))
or
while [ $(($(date +%s) - $(stat -c %Y File_`date +%F`*.xml))) -gt 60 ]; do
Should I just do a find for a file that was changed in the last 5 minutes, then have the stat check to see if it was done in the last 60 seconds to make sure that I'm only running against one file?
Thanks again for your help.
Last edited by EclipseAgent; 02-12-2009 at 04:53 PM.
to get the latest/most recent file. If the dir has files other than your xml file, you'll have to spcify the correct pattern to the ls eg
Code:
file=`ls -t *.xml |head -1`
What happens if there are more then 1 file, modified in the last minute with the original stat command? since it's just looking for anything greater then 60 in say file*? hummmm
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.