Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Send email if latest file in a directory is older than 2 hours

Shell Programming and Scripting


Tags
shell, solved, unix

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
simpltyansh simpltyansh is offline
Registered User
 
Join Date: Mar 2014
Last Activity: 22 May 2017, 2:12 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Send email if latest file in a directory is older than 2 hours

I have a objective of Sending email if latest file in a directory(excluding files of sub-dirs) is older than 2 hours.

eg :
Code:
 ls -ltr
 drwx--x--x 2 abcde abc 256 2017-02-07 20:10 Mail
-rw-rw-r-- 1 abcde abc 1170 2017-02-24 17:30 test
-rw-rw-r-- 1 abcde abc 356 2017-03-09 18:00 xyz.csv
-rw-rw-r-- 1 abcde abc 501 2017-03-09 18:05 abc2.csv
-rw-rw-r-- 1 abcde abc 415 2017-03-09 18:06 abc.csv
-rw-rw-r-- 1 abcde abc 34150 2017-03-09 18:39 test.csv
-rw-rw-r-- 1 abcde abc 119 2017-04-27 20:52 hotfix.ini



hotfix.ini is latest file in the directory, so I wish to compare the timestamp of this file with the current time of system and if file is older than 2+ hours then send mail with the time stamp of file:
hotfix.ini

It will be mostly a shell script which will run in background every 5 mins and send mail only if latest file(based on timestamp) is older than 2 hours. else would go back to sleep again for 5 mins.

Last edited by simpltyansh; 1 Week Ago at 03:26 PM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 29 May 2017, 6:41 AM EDT
Location: NM
Posts: 11,071
Thanks: 526
Thanked 1,060 Times in 983 Posts
Please tell us what UNIX you are using and what shell. In this case it makes a big difference. Linux has better date/time tools than Solaris for example. So the answer to your question will be different for different OS/shell.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 May 2017, 6:37 AM EDT
Location: Aachen, Germany
Posts: 10,830
Thanks: 272
Thanked 3,324 Times in 3,061 Posts
For just the time check, try

Code:
[ "$(find . -cmin -120)" ] || echo mail -s "subject one" recipient@url.com

For the youngest file's time stamp, your attempt should work.
    #4  
Old Unix and Linux 1 Week Ago
simpltyansh simpltyansh is offline
Registered User
 
Join Date: Mar 2014
Last Activity: 22 May 2017, 2:12 AM EDT
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
Please tell us what UNIX you are using and what shell. In this case it makes a big difference. Linux has better date/time tools than Solaris for example. So the answer to your question will be different for different OS/shell.
Using AIX and KSH

---------- Post updated at 02:24 PM ---------- Previous update was at 08:12 AM ----------

Quote:
Originally Posted by RudiC View Post
For just the time check, try

Code:
[ "$(find . -cmin -120)" ] || echo mail -s "subject one" recipient@url.com

For the youngest file's time stamp, your attempt should work.
Hi Rudi,

Can you help me with the complete script, I didn't get your solution.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Loop multiple directory, find a file and send email penchev Shell Programming and Scripting 1 11-02-2015 12:24 PM
Script to send email after comparing the folder permissions to a certain permission & send email nairshar Shell Programming and Scripting 2 12-07-2010 08:22 PM
Shell script: If a file stays in a particular directory more than 30 min send an email muraliinfy04 Shell Programming and Scripting 8 03-29-2010 10:46 PM
Move the latest or older File from one directory to another Directory pp_ayyanar Shell Programming and Scripting 1 03-24-2010 03:22 PM
unix command/s to find files older than 2 hours in a directory Presanna Shell Programming and Scripting 6 11-20-2007 08:22 AM



All times are GMT -4. The time now is 07:39 AM.