Calculate age of a file | calculate time difference

Hello,

I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes...

I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes.

To do so, I have created two variables:

CURRTIME=`date +"%H%M"`
FILETIME=`ls -l $APSYS_DAT/currency.txt | awk '{print $8}' | awk -F: '{print $1 $2}'`

Now, I could calculate: echo $CURRTIME - $FILETIME | bc

But it doesnt calculate it as time... 1305-1255 gives me 50 and not 10 minutes.

How can I calculate with time?

Thanks for your help!

Best regards

Rolf

This uses the stat command, which I am not sure is available in your shell. It works by converting everything to epoch time, and in this case is checking for files within 3600 seconds (60 minutes). But, perhaps playing with these commands might get you started.

Hello Joeyg,

Thanks for the hint to use epoch time.

It was a little bit tricky to find the current epoch time on solaris, as
date +%s doesn't exist on solaris.

But the following line gives me the current epoch time:

truss date 2>&1 | grep time | awk '{print $3}'

Now I just need to find out, how I can print the epoch time, when the file was created.

Unfortunately, the stat command, you used in your script, doesn't exist on solaris.

Any ideas, how I could find the epoch time of the file creation?

Thanks and best regards

Rolf

I seem to remember using the stat command, under bash, while on a Solaris machine at an earlier job. Hmmm....
Going to have to think a bit.

Perhaps others can offer a suggestion?

Will you be running your monitoring script from "cron"? If so, how frequently and during what hours and on what days of the week?

It is easy to write a script to run from cron which maintains a reference file with a timestamp 20 mins old but remember that if run once a minute such a cron would run 1440 times a day. If you really don't need to be that accurate, could you check every 10 mins or more?

I don't believe *nix systems store 'creation' time. Only changed, modified, and accessed.

AlphaLexman is correct. The "last modified" timestamp is the most useful and is the one used by "ls -la" and "find -mtime" etc..
The "-ctime" timestamp is the timestamp when the inode was last changed. Some backup software changes this timestamp to mark the file "backed up".

Earlier I was trying to avoid the epoch date arithmetic or writing a "C" or "perl" (or whatever) program.

There are many techniques to find files which are less than 20 minutes old even if you don't have the GNU version of "find" - which offers this sort of search as standard.

Let's find out what you are trying to do in more detail.

Good morning,

I don't use the crontab for this monitoring file... it's called from a scheduler of the application installed on this server. I run it monday to friday between 07:00 and 19:00.
I want to run the script every 5 minutes ...

I can use every date available ... the file is written once and will be overwritten when a new file arrives. There is never a change on the file.

The only thing I need is to check if the file is older than 20 minutes.

If there is a better or easier way than use the epoch time, it's OK for me...
I don't care which technic is used, I just need a solution :slight_smile:

Thanks and best regards

Rolf

Perl one-liner to get file age in minutes....

# ls -l /etc/passwd
-rw-r--r--   1 root     sys         1142 Feb 14 11:35 /etc/passwd
# date
Mon Feb 14 12:47:08 EST 2011
# perl -e 'printf "%d\n", (time - ((stat(shift))[9]))/60;' /etc/passwd
72
#

You can get rid of the "/60" to get the age in seconds.

Or...

perl -e 'printf "%d\n",(-M shift)*24*60' /etc/passwd
1 Like

Thank you very much, it works perfect!

I see, I have to learn perl :slight_smile:

Best regards

Rolf