Redirect crontab output to email

Solution 1:

The cron entry you have there is redirecting both STDOUT and STDERR to a file: /var/log/CRONLOGS/testing_prod.log.

The simplest method to do what you want would be to run mailx at the end of the cron job, using indirection to send that file:

00 06 * * * /usr/bin/php /home/user/myapp/testing.sh -e prod 1>> /var/log/CRONLOGS/testing_prod.log 2>&1; mailx -s "Cron output" [email protected] < /var/log/CRONLOGS/testing_prod.log

However: take note that your cron job is appending to the log file, so unless it's being purged of old entries (or rotated) regularly, you'll receive all previous output as well as new output. It would probably be best to create a small shell script to write to a temporary log, mail it, then append it to the mail log:

#!/bin/sh
/usr/bin/php /home/user/myapp/testing.sh -e prod 1>> /tmp/testing_prod$$.log 2>&1
mailx -s "Cron output" [email protected] < /tmp/testing_prod$$.log
cat /tmp/testing_prod$$.log >> /var/log/CRONLOGS/testing_prod.log
rm /tmp/testing_prod$$.log

This has the benefit of making the crontab look tidier:

00 06 * * * /home/user/myapp/cronscript.sh