Logging ALL stderr output of crontab to file

cronio-redirectionlogs

For example, I can log stderr of one script in this way:

* * * * * run_script.sh > /var/log.txt 2>&1

But I want to log stderr of all scripts in my crontab. I can append > /var/log.txt 2>&1 to all scripts, but it's not good if I have hundreds of scripts in cron. Is there another, more simple way to do this?

Best Answer

In crontab, you can set MAILTO to point to a mail alias that runs a script. That script would accept a mail message, strip off the headers and other goop, and log the remainder with logger. Since all cron script output is sent to the address specified by MAILTO, you'd capture everything.

Example: in crontab

MAILTO=myalias

In /etc/mail/aliases (assuming you're using sendmail)

myalias:"|/usr/local/bin/my-processing-script.sh"

and have the script strip off the mail headers and process the cron output.

Related Question