Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to check for the presence of files older than 30mins 2

Status
Not open for further replies.

gringomike

Technical User
Aug 6, 2003
148
GB
Hi All,

I have a script that runs between 8am and 7pm and polls every 3 minutes, picking up files and processing them into an application.
I would like it to check for the presence of any file older than 30 minutes then email the administrator to let them know that manual intervention is required.

My question is this - how do I create a variable that is "current date - 30minutes"?

Thanks in advance!
 
One method:
Code:
#!/bin/sh
TIMESTAMP=/tmp/timestamp
find . -type f ! -newer $TIMESTAMP | xargs process
touch $TIMESTAMP
Where process is the command you wish to apply to each file. Place this script in cron, to be run every 30 minutes. The last action of this script is to reset the timestamp file, so at the next invocation this timestamp will be exactly 30 minutes old. This can then be used for your comparison.
Cheers, Neil
 
how do I create a variable that is "current date - 30minutes"?
Try something like this (in ksh)
typeset -i localOffset=$(echo $TZ |
sed 's![^-0-9]*\([-0-9]*\).*!\1!')
now=$(date)
now_30=$(TZ=X${localOffset}:30 date)
echo "$now\n$now_30
You may use a format argument with the date command to get a date value suitable for the touch command.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks,

PHV

This works but doesn't take into account British Summer Time (BST). $TZ is equal to "GB" so the sed function which should apply a modifier later in the script doesn't.

$ date -u
Wednesday June 2 14:06:46 GMT 2004
$ date
Wednesday June 2 15:06:55 BST 2004

typeset -i localOffset=$(echo $TZ | sed 's![^-0-9]*\([-0-9]*\).*!\1!')
$ now_30=$(TZ=X${localOffset}:30 date)
$ echo "$now_30"

Wednesday June 2 13:35:25 X 2004

As you can see it works perfectly for GMT but not BST, any ideas how to generalise it so it can cope with BST and the change back to GMT.

Many thanks.
 
Provided that the fist half-hour of the day is not to be considered, here the awk way:
TimeStamp=$(date '+%m %d %H %M' | awk '{
m=60*$3+$4-30;printf "%02d%02d%02d%02d",$1,$2,int(m/60),m%60
}')

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Ooops, didn't spot the requirement that it had to be run more frequently. Here's a very unelegant way, that won't work until after 00:30 each day:
Code:
#!/usr/bin/ksh

typeset -i month=$(date +%m)
typeset -i day=$(date +%d)
typeset -i hours=$(date +%H)
typeset -i minutes=$(date +%M)
typeset -i seconds=$(date +%S)

now=$(printf "%02s%02s%02s%02s%02s" $month $day $hours $minutes $seconds)
echo "now: $now"

# skip if < 00:30
[[ $hours -eq 0 && $minutes -lt 30 ]] && exit

# remove 30 minutes
let minutes-=30
if [[ $minutes -lt 0 ]] ; then
    let minutes+=60
    let hours-=1
fi

earlier=$(printf "%02s%02s%02s%02s%02s" $month $day $hours $minutes $seconds)
echo "earlier: $earlier"
Ugly.... [upsidedown]
 
[perl blasphemy]

If you have perl...

TimeStamp=$(perl -e '@f = localtime(time() - 1800);
printf "%02d%02d%02d%02d", $f[4]+1, $f[3], $f[2], $f[1]')

[/perl blasphemy]
 
Well if we can use perl ;-)
Code:
#!/usr/bin/perl -w
use File::Find;
use File::stat;
my $now = time;
my $age = 30 * 60;
sub wanted {
    if (-f) {
        my $st = stat($_);
        if ($now - $st->mtime >= $age) {
            print "$_\n";
        }
    }
}
find(\&wanted, ".");
Cheers, Neil
 
Thanks everyone!

We don't use perl but the solutions from PHV and toolkit are just what we're looking for!

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top