Have cron email output to MAILTO based on exit status

php /path/to/script.php > logfile || cat logfile; rm logfile

which dumps standard output into logfile and only outputs it if the script fails (exits non-zero).

Note: if your script might also output to stderr then you should redirect stderr to stdout. Otherwise anything printed to stderr will cause cron to send an email even if the exit code is 0:

php /path/to/script.php > logfile 2>&1 || cat logfile; rm logfile

Have you considered chronic from moreutils. I think it does exactly what you want:

chronic runs a command, and arranges for its standard out and standard error to only be displayed if the command fails (exits nonzero or crashes). If the command succeeds, any extraneous output will be hidden.

In recent verisions, there is a -e switch to also show the full output if anything has been written to stderr.


Since the output is generated before the exit status is known, you'll have to store it somewhere.

One possibility is to store it in a shell variable:

output=$(php /path/to/script.php)
if [ $? -ne 0 ]; then
  printf "%s\n" "$output"
fi

This doesn't completely preserve the script's output (it removes trailing blank lines), but that's ok for this use case. If you want to preserve trailing blank lines:

output=$(php /path/to/script.php; ret=$?; echo a; exit $ret)
if [ $? -ne 0 ]; then
  printf "%s" "${output%a}"
fi

If there's potentially a lot of output, you might prefer to store it in a temporary file instead:

output_file=$(mktemp /var/tmp/script.XXXXXXXXXX.out)
php /path/to/script.php >>"$output_file"
ret=$?
if [ $ret -ne 0 ]; then
  echo "script.php failed (status $ret), see the output in $output_file"
fi

Tags:

Bash

Cron