Link to home
Start Free TrialLog in
Avatar of jonke
jonke

asked on

Find out if a file is more than five minutes old

I'm trying to write this in ksh.

Anyone now any simple ways of finding out if a file is more than five minutes old?

What I really want to avoid is having to get the output from date, and then awking out the minutes - and doing a load of 'if' statements depending on whether it just gone past the hour etc. etc. That would all be a bit messy.
Avatar of ahoffmann
ahoffmann
Flag of Germany image

# if you have GNU's find
find path-t0-file -mmin 5
# otherwhise
touch 5minutefile && sleep 300
find path-to-file -newer 5minutefile
Avatar of yuzh
yuzh

Hi jonke,
   
    If you don't want to use "date" and awk/cut command, you should accept ahoffmann , as answer.

    If you allow to use "date", then you can use the followings as examle, for you question:
   
    CHH= `date +%H`
    CTM=`date +%M`
     CSS=`date +%S`

     if  [ $CMM -lt 5 ] ; then
           $CMM = $CMM + 60 - 5
     fi

     MYM= $CTM - 5

      touch  -t `date +%m%d`${CMM}${CSS} dummy
      find /search-path -newer dummy -print

      You can write a better script by checking out the 0 hour and < 5 min
condition.

       If you write a script like this, it will have no sleep time and run fast than ahoffmann's solution, but you need to do more work as the trade off.
     
     
I would do a:
   $CT=`date +%H%M`
   MYM=$CT - 5
   MHM=$CT - 40
   MYM=`expr $MYM ':' '\(..[0-5].\)' '|' $MHM`
   touch  -t `date +%m%d`${MYM} dummy
oops, must be  CT=  instead of  $CT=
The above two comments just for demo, to make it work as you reqired, you need to do a bit more. eg. when min < 5, you have to use hh=$hh -1.
"just for demo," and to show that the previous comment contains typos (I assumed)
sorry for being sooo picky ;-)
Here's and easy-peasy way:

touch /t-300
sleep 300
find / -newer /t-300 -ls

Regards, Nisus
http://www.omnimodo.com

Nisus, your solution finds **newer** not older files !
BTW, why should I wait 5 minutes to see such a result when it can be done in less than a second?
The -newer option find files that have been modified since the timestamp of the file I touched.

The 'sleep 300' waits for the five minutes before we run the find, thereby showing us all files modified in the last five minutes.

It's a neat solution.

Avatar of jonke

ASKER

All good answers. However - I was unable to use any of them for the problem I was having. This was mainly due to the fact that the question I asked wasn't particularly phrased well.

The explanation behind the question is as follows: I was working within a cluster environment, where I needed to create a monitor script to monitor a no. of processes. These processes were self monitoring too - and had the ability to restart themselves. The way the cluster software works, is that it runs a monitor script periodically, and checks for the following processes. I only wanted the monitor script to give a failed response once a process had been dead for over five minutes (Thus giving it a chance to self restart).

The idea I was using, was that when the monitor script runs and notices that a process is missing, it will set a control file. For future monitoring processes, it will look for the control file, and if it is there, it is > then 5 minutes old and the process is still not running, then it will retuen a failed reponse.

The monitor script needs to return fairly promptly, hence why I am unable to put any long sleeps in it. I guess I could still potentially use a find command with the -newer flag, and if I get a negative response, then call a fail. I will investigate and get back to you.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany 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
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by ahoffmann

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster
Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange