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.
Asked
Active
Viewed 1,728 times
3
-
You should probably look at monitoring software – Gilles 'SO- stop being evil' Mar 14 '13 at 23:32
1 Answers
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
-
-
1If 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 theecho
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 -
-
@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 (thenow=
line) -- please try that. – Flup Mar 14 '13 at 12:12 -