Script to compare file date to system and email alert if file older than 1 day

Posted on 2005-04-26
Last Modified: 2010-08-05
Hello everyone,

I need a solution to my problem either in Linux or Windows

     1. A script to run as a cron job to check the date/time of certain files located in certain directories and if the files are older than 24 hours to send an alert email to me.

     2. Windows scheduled tasks monitor to alert me via email if task status contains errors or did not run properly.

Below is the scenario:

Server: RH9
Path: /home/user

there is a utility that creates these files and uploads them to this server via FTP, but the utility is buggy and runs on Windows. It runs at 4:30 every day from the "scheduled tasks" in Windows 2000 server but once in a while it crashes while parsing the data (can't handle exceptions :-) )

I can catch the problem in the "scheduled tasks" if the utility crashes and outputs errors, but have not been able to find an application / VBS script that sends emails if a job ends abnormally

I am no programmer and it seems like a simple script in linux should suffice to notify me, so I can correct the problem. Any recommandation for either solution (Windows or Linux) can help.

Closest thing for windows is at this link:
Question by:Brainstormer
    LVL 2

    Expert Comment

    Hi Brainstormer,

    oldfiles=`find . -type f -mtime +1 -maxdepth 1 -print` ; if [ -n "$oldfiles" ] ; then echo $oldfiles |  mail -s "Old files" ; fi

    To explain...
    The find command starts in the current directory (".") - change that to the directory which will contain your files
    It looks for files (-type f) [not directories]
    It looks for items which are more than 24 hours old (-mtime +1)
    It looks only in the current directory, not subdirectories (-maxdepth 1) - remove that to search all subdirectories, or change to another number to go only that depth
    It prints any found files

    The output of the find command is captured with `` and saved into the environment variable oldfiles

    If oldfiles is not empty (if [ -n $oldfiles ]) then the mail command is started up with the specified subject and destination address, and the actual file names are echoed into the body of the email.

    Hope this helps,

    LVL 6

    Author Comment

    nikkilocke, I should note that there are many other files in /home/user which are not changed and date back to 2001. I am only interested in the three I mentioned, which change ever day, so the script needs to be specific.

    LVL 2

    Accepted Solution

    Oh, I didn't realise. You need to add
    -name '<regular expression matching only those files you want>' to the find command.

    Say, for example, that the files you wanted were f*.txt

    oldfiles=`find . -name 'f*.txt' -type f -mtime +1 -maxdepth 1 -print` ; if [ -n "$oldfiles" ] ; then echo $oldfiles |  mail -s "Old files" ; fi

    If you can't devise a suitable regular expression, try listing the names as in

    oldfiles=`find . \( -name file1 -or -name file2 \)  -type f -mtime +1 -maxdepth 1 -print` ; if [ -n "$oldfiles" ] ; then echo $oldfiles |  mail -s "Old files" ; fi

    Hope this solves your problem,

    LVL 6

    Author Comment

    I will test the following:

    oldfiles=`find . \( -name file1.csv -or -name file2.csv -or -name file1.xml \)  -type f -mtime +1 -maxdepth 1 -print`  # all one line
    if [ -n "$oldfiles" ]
          then echo $oldfiles |  mail -s "Old files"
          echo " The files are recent, everything OK"
    LVL 6

    Author Comment

    this worked perfectly !

    Thanks nikkilocke

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    15 Experts available now in Live!

    Get 1:1 Help Now