?
Solved

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

Posted on 2005-04-26
5
Medium Priority
?
420 Views
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
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
0
Comment
Question by:Brainstormer
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:nikkilocke
ID: 13867686
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
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 13867798
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,
0
 
LVL 2

Accepted Solution

by:
nikkilocke earned 1000 total points
ID: 13868518
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" your.email@your.domain ; 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" your.email@your.domain ; fi

Hope this solves your problem,

Nikki
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 13868674
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
0
 
LVL 6

Author Comment

by:Brainstormer
ID: 13868860
this worked perfectly !

Thanks nikkilocke
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 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