2

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.

2 Answers 2

4

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

[email protected]
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 [email protected] 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.

0
2

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .