Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am new to shell scripting. How would I write a script which can send an alert mail if there is no entry in the log file for more than 3 hours.

share|improve this question
    
You should probably look at monitoring software –  Gilles Mar 14 '13 at 23:32

1 Answer 1

A bit convoluted, but it works:

#!/bin/bash

now=`date +%s`
max_age=10800 # 3 hours in seconds

if [ $(($now - `stat -c '%Y' $1`)) -gt $max_age ]; then
    echo "file hasn't been updated in $max_age seconds"
fi

Call the script with the filename as the only argument.

share|improve this answer
    
I might be missing something, but where is this sending an email? –  MaxMackie Mar 14 '13 at 10:41
1  
If you run this as a cron job, you'll get email whenever it generates output. Alternatively, run it by hand and add something like | mail -s 'Oh no!' you@your.domain to the echo line. –  Flup Mar 14 '13 at 10:44
    
Hi Max, Thanks for your help....But still i'm not able to execute the script successfully...The error which i'm getting is printf: `(': invalid format character –  Abdul Mar 14 '13 at 11:13
    
@Flup, ahh right. My bad. –  MaxMackie Mar 14 '13 at 11:19
    
@Abdul Make sure that you have the quotes exactly as they are above. Also make sure you are running the script with bash. –  Flup Mar 14 '13 at 11:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.