Conditionally Changing Subject Line of Email Sent by Cron Job

William Park opengeometry-FFYn/CNdgSA at public.gmane.org
Mon Aug 9 19:02:21 UTC 2010


On Mon, Aug 09, 2010 at 02:39:40PM -0400, CLIFFORD ILKAY wrote:
> Hi,
> 
> I have a Python script to update RSS feed subscriptions that I invoke in 
> a BASH script via cron. If I invoke the shell script manually, I'll see 
> this:
> 
> cilkay at jupiter:~$ /home/cilkay/virtualenvs/test/bin/update_feeds.sh
> Your Industry News
> Alberta Oil Magazine
> Oilweek - Canada's Oil and Gas Authority
> Hydro World
> Your Oil and Gas News
> 
> Occasionally, one of the feeds will be malformed and the script will 
> output a Python traceback. I want cron to send emails to me regardless 
> of success or failure of the script but I want the subject line to be 
> "SUCCESS" or "FAILURE" so that I only have to pay attention if the 
> subject line is "FAILURE". The crontab entry for this script is below.
> 
> */17 * * * * /home/cilkay/virtualenvs/test/bin/update_feeds.sh 2>&1 | 
> mail -s "FE Results" cron_results-BYqcrMrCQOc at public.gmane.org
> 
> At the moment, as you can see, I have the same subject line regardless 
> of the return code. How can I make the subject line "SUCCESS: Feed 
> Updater" or "FAILURE: Feed Updater" depending on the return code of the 
> BASH script?

Since return code can only be tested after it returns, you have to do
this in 2-steps.  Try something like,

    */17 * * * * /home/cilkay/virtualenvs/test/bin/update_feeds.sh >/tmp/x 2>&1; if test $? -eq 0; then SUBJECT="SUCESS"; else SUBJECT="FAILURE"; fi; mail -s "$SUBJECT" cron_results-BYqcrMrCQOc at public.gmane.org < /tmp/x

First command runs, second command tests for its return code, and
finally third command sends the mail.
-- 
William

--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list