calculating the time difference, when the script was executed and the currenent file

Hi,

I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert.
I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the message.

Hi,

I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert.
I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the message.

stat -c %X ==>time of last access
stat -c %Y ==>time of last modification
stat -c %Z ==>time of last change

Thus, if you set a variable to capture the returned number, you can compare it to a number for now (i.e. touch a file and get similar value).

ch_file1=$(stat -c %Y file1)
touch file2
ch_file2=$(stat -c %Y file2)

then see if they are different by more than 300 [300 seconds = 5 minutes]

> file_diff=$((ch_file2-ch_file1))
> echo $file_diff
533

I made the script, but its giving the error

stat: not found.

Hmm "stat" is not a HP UX command. It's on some Linux distributions but not a portable unix command.

Personally I'd use cron to run the script every 5 mins during the monitoring period.

Start the cron 5 minutes earlier than the start of your monitoring period.
Define a timestamp filename containing todays date:
YYYYMMDD="`date +%Y%m%d`"
my_timestamp="my_prefix${YYYMMDD}"

At the start of the script if the timestamp file does not exist, create it with unix "touch" and exit.

On the second and subsequent invocations the file will exist. You can then easily check whether the alert file timestamp is more than 5 minutes old with "find ... -type f ! -newer $my_timestamp ... ".

At the end of the script "touch" the timestamp file ready for next time.

You will need some code to delete old timestamp files, so choose a unique prefix for the timestamp filename.
Beware: Recursive alert log checkers can easily generate multiple alarms for one incident unless you code to deal with this.

I cannot use the cron,as i am running the script through automation tool.
Also /tmp/nitin -type f -newer timestamp.sh is running fine but /tmp/nitin -type f! -newer timestamp.sh is giving error bad -type f!.

Is there any other way, to know that the particular file is older than 5 min.

Space character needed between the "f" and the "!".

If accuracy (to one minute) is not important you could run a cron every minute which maintains a file which is at least 5 minutes old.
There will be little performance impact but make sure you have enough disc space for 4320 (24 * 60 *3) extra lines in your cron log per day.

#!/bin/ksh
# 5minsago.sh Maintain file called 5minsago
# Run from cron every minute. Effective after 6 mins.
#
cd /var/tmp
mv 4minsago 5minsago 2>/dev/null
mv 3minsago 4minsago 2>/dev/null
mv 2minsago 3minsago 2>/dev/null
mv 1minsago 2minsago 2>/dev/null
mv 0minsago 1minsago 2>/dev/null
touch 0minsago

Then in the script called by your automation program:
find <dir> ! -newer /var/tmp/5minsago -print|while read OLDALERTFILE
do
<process the old files>
done

If accuracy is important you need a "C" program.

ps. I've avoided posting a shell script to subtract 5 mins from the current time because such a script is very long.