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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Abdul
  • 1,731

1 Answers1

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.

Flup
  • 8,145
  • I might be missing something, but where is this sending an email? – nopcorn 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. – nopcorn 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
  • @Max, ya i'm running the script with bash & the codes are exactly same what you have given..But still i get the same error...Could you help me to figure out what the mistake is???? – Abdul Mar 14 '13 at 12:07
  • My apologies, it appears I might be running a more recent version of bash. I've made a small change above (the now= line) -- please try that. – Flup Mar 14 '13 at 12:12
  • @Max,ya its working fine now :)Thanks a lot :) – Abdul Mar 14 '13 at 12:41