|
Mail from cron job abnormalities |
|
|
|
|
Written by Carl Friis-Hansen
|
|
Friday, 31 October 2008 20:43 |
|
The aim is to ensure that root is emailed in case of abnormalities in cron jobs:
The right way to deal with this is to change your script so that it pipes its results into (say) grep:
#!/bin/sh killall -q fetchmail /usr/bin/fetchmail | grep -iv "No mail for "
That way there will be no output generated if everything works as expected.
With egrep you can scan for more than one expected result. Or you could use a chain of greps.
killall outputs nothing if it successfully kills things. "-q" tells it not to report that no processes were killed. If anything else happens, you will be informed. There may be a similar switch for fetchmail, which would save using grep.
If expected messages are going to stderr instead of stdout you have to use a syntax that is slightly less well known. For example, to capture the "no process killed" message from killall (instead of using "-q"):
killall telnet 2>&1 | grep -iv "no process killed"
One day your script will output something different: "fetchmail: Oops! Major problem! You have eight hours to act before your hard disk is wiped and your pets are shaved!"
Because that doesn't match the regexp, it will be output and thus mailed to you in time for you to save the day.
|