3

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.

2

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.

  • I might be missing something, but where is this sending an email? – n0pe 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. – n0pe 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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