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

Posted on 2007-07-26
Last Modified: 2010-03-05
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\";
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;

Question by:mhorrocks
    1 Comment
    LVL 84

    Accepted Solution

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now