1834078 Members
2547 Online
110063 Solutions
New Discussion

Check time

 
SOLVED
Go to solution
Trenta Angelo
Contributor

Check time

Hi all,

I???ve a problem,
With this command: ls ???ltr events/e*/* |awk ???NR==1 [print $8]???

I find the oldest file in a directory.

This file is removed from time to time,
And I need to know if It is becoming is older than 20 minutes
I can compare time stamp with current date(date command)

But I don???t know thow to create script to understand
If this file is older than 20 minutes.

Regards, Angelo
I will work harder, HP-UX is always right
6 REPLIES 6
Peter Kloetgen
Esteemed Contributor

Re: Check time

Hi Trenta,

all you need is a reference file which you need to compare with your found file.

man touch

touch reference_time filename

With this command you make a file with a reference time. Use this file with find command to check the age.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Carlos Fernandez Riera
Honored Contributor

Re: Check time

James Beamish-White
Trusted Contributor

Re: Check time

Hi Angelo,

Well, you can get the similar date from from date '+%H:%m'. Try splitting the hour and minute and using expr to figure out if the difference is less than 20.

Cheers,
James
GARDENOFEDEN> create light
Paula J Frazer-Campbell
Honored Contributor

Re: Check time

Hi


Using your command:-



#!/bin/sh
# File Watcher
# Log file /tmp/fwatcher
fdate=`ls ???ltr events/e*/* |awk ???NR==1 [print $8]???`
date >>/tmp/fwatcher
echo $fdate >>/tmp/fwatcher
mailx -s "File Watcher log" (Your email address)


Cron it to run when you require and monitor the log file.

Quick, easy and mailed to your desktop.

Paula



If you can spell SysAdmin then you is one - anon
Chris Wilshaw
Honored Contributor

Re: Check time

This is an extract from a perl program I use for the same sort of thing;

------------------------------------------

#!/usr/contrib/bin/perl

$now = time;

foreach $filename (@ARGV) {

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $filename;

$age = ($now - $mtime) / 60 ;

if ( $age > 20 ) {
printf ("WARNING: %s is %d minutes old\n", $filename, $age);
}

}

---------------------------------------

All you do is run the script, with the file(s) you want the age of as parameters

eg: how_old.sh

Where is the name of the file that you've already obtained.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Check time

Chris, please check if you are /realy/ using /usr/contib/bin/perl, which is likely to be a *very* outdated perl version 4.036

Check if you've also got /opt/perl/bin/perl, which should be version 5.6.1

--8<---
#!/opt/perl/bin/perl -w

use strict;
my $now = time;

foreach my $filename (@ARGV) {
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $filename;

$age = ($now - $mtime) / 60 ;

if ($age > 20) {
print "WARNING: $filename is $age minutes old\n";
}
}
-->8---


how_old.sh would be a very misleading name for a perl script. Please us .pl as extension, so others will still know what to expect.

eg: how_old.pl

There have been many more threads that look like this one, and the perl solution could be made much more flexible if you use the standard module File::Find.

The age of a file can also (and much easier) be determined with the -M and -A operators (age of the file since the start of the script measured in (fractions of) days

-M $file > 4 and die;
Enjoy, Have FUN! H.Merijn