Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 3 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

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

This will send an e-mail to when the stirng "Error: " occures in the output of 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

Your Answer


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.