Aug 17, 2011
tom

Linux script to find time difference and send an email if need

Question

I’m not an expert in writing shell scripts but also I’m looking for a very specific solution.

OS: CentOS release 5.2 (Final)

I’ve a Java standalone which keeps writing (all System.out.println) to a log file. For some unknown reason, this Java standalone stops working at some point of time in my server and eventually logs writing also stops working.

I want to have a script which checks the last modified date & time of the log file with current date & time in the server. If the time difference exceeds more than 5 minutes, I want to send an email immediately to my recipients list. This way I’ll come to know when this Java standalone has stopped working.

I’ll move this script to crontab and make it run for every 1 minute, so that this whole process is automated.

Log file location: /usr/local/logs/standalone.log

Answer

however, i was bored… something like:

#!/bin/shSUBJECT="blah is more than 5 minutes old!"
EMAIL="email@example.com"
MSG="/tmp/emailmessage"MOD=`stat --printf=%Y $1`
NOW=`date +%s`
DIFF=$(($NOW-$MOD))
if [ $DIFF > 300 ]; then
    echo "$1 is more than 5 minutes old!" > $MSG
    mail -s "$SUBJECT" "$EMAIL" < $MSG
fi

Related posts:

  1. Send an email notification from a script
  2. backup email log script spams me: crazed BASH expansion
  3. Calculate highest time difference between log message in /var/log/messages
  4. Unix mail program CAN’T send email but crontab email agent CAN
  5. Trying to send an email for first time from my local server

Leave a comment