User1
User1

Reputation: 41163

How to get notified when a script didn't run

I have a bash script that runs on a machine (called lazyguy) that is usually turned off. Every night, lazyguy wakes up at a known time programmed in the BIOS, does a bunch of processing and inserts data to its Postgres database. Then, it goes to sleep (database and all).

How can get notified when the script did NOT run? Ideally, I'd like to get an email notification if nothing was inserted to the database or if the script exited with a non-zero return value. I definitely need to check for both conditions in case lazyguy ignored his alarm and slept in..again.

I have another machine (taskmaster) that is always running but has limited resources. Some thoughts:

Which approach is better? Is there an easier way to do this? Which technologies would you use? I'm thinking maybe some mix of crontab, mailx, logger, and maybe rsyslog.

Upvotes: 1

Views: 421

Answers (4)

Daenyth
Daenyth

Reputation: 37441

I'm surprised no one has mentioned this yet: When a script run from cron exits abnormally, it sends an email to the mail address specified in /etc/crontab. It includes all output of the script.

Upvotes: 1

Roman M
Roman M

Reputation: 12408

You can send your self an email through a command line with the mail command. Use this link http://www.amirwatad.com/blog/archives/2009/03/21/send-email-from-the-command-line-using-gmail-account/ to set it up. you can set a special account for this.

Upvotes: 0

mykhal
mykhal

Reputation: 19895

i would prefer your first, more decentralized method (e.g. lazyguy would do some request on a simple HTTP server running on taskmaster after successful job, the server would update the timestamp stored on the disk, which would be checked by cron script), but the the second one has an advantage - when you decide to change the lazyguy scheduling (and so a check scheduling), you would do it from a single place - taskmaster.

Upvotes: 1

mezzie
mezzie

Reputation: 1296

if lazyguy can print out something to a file after all of his chores are done, then it would be great.

          • rm -rf /tmp/lazyguy.log; /lazyguy.sh > /tmp/lazyguy.log

then have another script run at another time to check if lazyguy did finished, still running or did not run at all.

to check your if lazyguy you can do something like

checkamdump(){
while ps ax | grep -v grep | grep "lazyguy.sh" > /dev/null
do
sleep $SLEEPTIMEOUT
done
}

Also, you can check lazyguy.log by adding this to your script

FILESIZE=stat -c %s /tmp/lazyguy.log
if [ $FILESIZE -gt 0 ]
then
mailx -s "Lazyguy is really lazy" [email protected] < /tmp/lazyguy.log
fi
Hope this helps, add your comment if you have any other questions

Upvotes: 0

Related Questions