LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with script for check for a file that has changed in the last minute (http://www.linuxquestions.org/questions/programming-9/need-help-with-script-for-check-for-a-file-that-has-changed-in-the-last-minute-704020/)

EclipseAgent 02-11-2009 07:46 PM

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?

chrism01 02-11-2009 07:55 PM

I'd use stat:

stat -c %y filename

see
man stat

EclipseAgent 02-11-2009 08:05 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)

Why wouldn't the find method work?

jan61 02-12-2009 11:15 AM

Moin,

better use the %Y option of stat, then you can substract the timestamp from the current timestamp:
Code:

intvl=$(($(date +%s) - $(stat -c %Y somefile.txt)))
if test $intvl -gt 60; then
  do_something
fi

Jan

EDIT: find would also work, but I think, that's a little bit oversized for checking a single file.

EclipseAgent 02-12-2009 11:17 AM

Quote:

Originally Posted by jan61 (Post 3441187)
Moin,

better use the %Y option of stat, then you can substract the timestamp from the current timestamp:
Code:

intvl=$(($(date +%s) - $(stat -c %Y somefile.txt)))
if test $intvl -gt 60; then
  do_something
fi

Jan

Ok,

i'll give this a shot but how would I then change this into a loop?

Would it be:

while [ $intvl -gt 60 ]; do
sleep 59
done
cp /file/tosomewhere

EclipseAgent 02-12-2009 11:21 AM

Awesome.. this worked:

Code:

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"


jan61 02-12-2009 11:34 AM

Moin,

of course you have to do the calculation again in every loop iteration - otherwise the $intvl value doesn't change.

A short way:
Code:

while [ $(($(date +%s) - $(stat -c %Y /tmp/file))) -gt 60 ]; do
  echo "I feel like sleeping"
  sleep 5
done
echo "I worked for once"

Jan

P.S.: Your response was faster than mine ;-)

EclipseAgent 02-12-2009 03:59 PM

Quote:

Originally Posted by jan61 (Post 3441214)
Moin,

of course you have to do the calculation again in every loop iteration - otherwise the $intvl value doesn't change.

A short way:
Code:

while [ $(($(date +%s) - $(stat -c %Y /tmp/file))) -gt 60 ]; do
  echo "I feel like sleeping"
  sleep 5
done
echo "I worked for once"

Jan

P.S.: Your response was faster than mine ;-)


Jan,

Just to add a little more "confusion" into the mix.

Here are the file names:

File_2009-02-12-02-00.xml

The stat I am using is:
stat -c %Y File_`date +%F`*.xml

with that said, if there are 2 files say:

File_2009-02-12-02-00.xml
File_2009-02-12-12-00.xml

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.

chrism01 02-12-2009 05:43 PM

Use

Code:


file=`ls -t|head -1`

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`

EclipseAgent 02-12-2009 06:39 PM

Quote:

Originally Posted by chrism01 (Post 3441621)
Use

Code:


file=`ls -t|head -1`

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

EclipseAgent 02-12-2009 07:46 PM

I got it all figured out.. I just did

file=`ls -t *.xml |head -1`

at the beginning and did a stat on $FILE .. then repopulated $FILE within the while loop..


All times are GMT -5. The time now is 05:15 AM.