LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-11-2009, 08:46 PM   #1
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Rep: Reputation: 30
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 08:49 PM.
 
Old 02-11-2009, 08:55 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I'd use stat:

stat -c %y filename

see
man stat
 
Old 02-11-2009, 09:05 PM   #3
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Original Poster
Rep: Reputation: 30
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?
 
Old 02-12-2009, 12:15 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
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.

Last edited by jan61; 02-12-2009 at 12:16 PM.
 
Old 02-12-2009, 12:17 PM   #5
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by jan61 View Post
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

Last edited by EclipseAgent; 02-12-2009 at 12:20 PM. Reason: whoops.. while do not while then
 
Old 02-12-2009, 12:21 PM   #6
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Original Poster
Rep: Reputation: 30
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"
 
Old 02-12-2009, 12:34 PM   #7
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
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 ;-)
 
Old 02-12-2009, 04:59 PM   #8
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by jan61 View Post
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.

Last edited by EclipseAgent; 02-12-2009 at 05:53 PM.
 
Old 02-12-2009, 06:43 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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`
 
Old 02-12-2009, 07:39 PM   #10
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by chrism01 View Post
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
 
Old 02-12-2009, 08:46 PM   #11
EclipseAgent
Member
 
Registered: Oct 2005
Location: California
Distribution: SLED 10, openSuSE 10.2, Ubuntu Drapper
Posts: 713

Original Poster
Rep: Reputation: 30
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script - how to check file size noir911 Programming 3 01-24-2009 10:17 AM
How can I make my script check to see if a file is empty? spiffytech Linux - Software 6 01-01-2006 10:35 AM
check file permissions in a script cambie Linux - General 9 09-22-2004 01:13 AM
Script to check for contents in a file darin3200 Programming 7 04-25-2004 06:26 PM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 09:30 AM


All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration