• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Open a file in perl script only if modified in last x minutes

In the following script, I open a log file.  What I need to do is only open the file if it has been modified or created in the last 15 minutes.

Reason:  Log file gets overwritten at least 5 times a day and I don't want to open a log file from a previous session.

use strict;

my $log = '/u/tmp/ftpupload_te.log';
my $emails = "anyone\@domain.com";
my $subject = "TECO FTP Upload Error";
my $subject1 = "TECO FTP Upload Success";
my $string1 = "File successfully transferred";
my $found;

open(LOG, ">$log");

open (FILE, "</u/tmp/mwh.log") || die "Cannot open file";

        $found ||=/$string1/
unless($found){print LOG "FTP Upload Error\n";}
unless($found){my $ret = `cat "/u/tmp/ftpupload_te.log" | mail -s "$subject" "$emails"`;}

close FILE;

close LOG;

1 Solution
die "file not modified in the last 15 minutes" unless  -M "/u/tmp/mwh.log" < 15/(60*24);

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now