I have a cron job running a php command like this:

php /path/to/script.php > dev/null

This should send only STDERR output to the MAILTO address. From what I gather the php script is not outputting any STDERR information even when its exit status is 1.

How can I get the output of the php command (STDOUT) and only send it to MAILTO if the exit status is non-zero?

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
  • This will also kick out for anything appearing in stderr, which doesn't necessarily mean there's an error (e.g., debug output). – hoffmanc Jan 4 '14 at 0:51

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"

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}"

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"
if [ $ret -ne 0 ]; then
  echo "script.php failed (status $ret), see the output in $output_file"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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