Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to run a test case via automation testing (sahi) , so I am running it repeatedly after 1 hour (via crontab).
What I want is that whenever my test case fails i should receive the email otherwise not. Right now I am receiving mail whether it passes or fails.
In short, can I send mail to a person depending upon the output I get in terminal.

I am using Ubuntu 10.10.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Pipelineing works in cron just as well as in bash. You could pipe the output to a script, that examines it and sends the mail. Or even easier use grep:

# in /etc/crontab

MAILTO=a.b@c.de
SHELL=/bin/bash

# m h dom mon dow user      command
30  * *   *   *   confus    /home/confus/bin/someCommand.sh | grep -A 11 -B 10 "Error:"

This will send an e-mail to a.b@.c.de when the stirng "Error: " occures in the output of someCommand.sh. In this case the text of the e-mail will be the output from 10 lines before and 11 lines after the occurrence of "Error: " (hence the -A for after and -B for before).

The mail is only send if the computer running cron has a working mail-server installed. A script to process the output is of course more flexible and considerably more work.

share|improve this answer
    
it working now..thanks a lot. –  Arpit Rawat Aug 31 '11 at 12:56

It sounds like either the exit code of your command is not being set correctly or that you are sending messages to stdout but not capturing/redirecting it. On a success, you should return 0 (exit 0). And on a failure return a positive number less than 256 (exit 2). Cron will detect the exit code and send email (MAILTO variable in crontab) on a non-zero exit code.

Also, if any output is sent to stdout or stderr, that is emailed to the user, regardless of the exit code.

See the cron(8) manpage for more information.

share|improve this answer
    
Thanks Arcege.. –  Arpit Rawat Sep 1 '11 at 4:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.