Link to home
Start Free TrialLog in
Avatar of Brainstormer
BrainstormerFlag for United States of America

asked on

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

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
files:
         file1.csv
         file2.csv
         file1.xml

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: http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/24594/24594.html
Avatar of nikkilocke
nikkilocke

Hi Brainstormer,

oldfiles=`find . -type f -mtime +1 -maxdepth 1 -print` ; if [ -n "$oldfiles" ] ; then echo $oldfiles |  mail -s "Old files" your.email@your.domain ; 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,

Nikki
Avatar of Brainstormer

ASKER

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.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of nikkilocke
nikkilocke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will test the following:


#!/bin/sh
# testdate.sh
#
#
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" your.email@your.domain
#
else
      echo " The files are recent, everything OK"
      exit
fi
this worked perfectly !

Thanks nikkilocke