1833431 Members
3214 Online
110052 Solutions
New Discussion

Testing for 1 hour old.

 
SOLVED
Go to solution
Fred Myers
Advisor

Testing for 1 hour old.

I am running a program to restart Java if it gets errors in the log file.

I would like to add a routine to restart java if the log does not get updated for at least one hour.

How do you go about comparing the time stamp of a file modification, to the current time, and see if it is at least 1 hour or more old?

Thanks
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Testing for 1 hour old.

Hi Fred:

You can do this in the shell with a combination of touch to create a file with a timestampo then find -newer (or -older) but a much cleaner way is to use this perl script:

fileage.pl -m -s 3600 myfile

The will silently return a 1 exit status if myfile has been modified within the last hour or 0 otherwise.

fileage.pl -m -s 3600 myfile
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "File was modified"
else
echo "File not modified"
fi

You can also add a -v argument to actually echo 1 or 0 to stdout. fileage.pl -u will display a full usage.


Regards, Clay

If it ain't broke, I can fix that.
Christopher Caldwell
Honored Contributor

Re: Testing for 1 hour old.

Language preference?

In C, perl it's pretty trivial.

Use fstat to get the file status information.
Time in Unix is an integral number counted in seconds. Make sure the difference between now and last modified is > 3600 (60 seconds * 60 minutes). Restart.
A. Clay Stephenson
Acclaimed Contributor

Re: Testing for 1 hour old.

Hi again Fred:

Oops my shell logic was wrong. The text description was correct but the example was wrong. It should be:
fileage.pl -m -s 3600 myfile
STAT=$?
if [ ${STAT} -eq 1 ]
then
echo "File was modified"
else
echo "File not modified"
fi

Notice that you have 1 second resolution and you can use -a for last access or -c for last create/chmod.



If it ain't broke, I can fix that.