I need my cronjobs to still continue to output errors in some files, but I also want them to be emailed at the same time. That doesn't seem to be possible without some tricks.
I found this but didn't helped me.
What would be the most simple way to do that ?
If I understand your question correctly, you want to send the errors (not output) to a logfile, and also send them via email.
To do this, use a combination of
tee
and stdout/stderr redirection.According to the cron(8) manpage, cron can handle the email for you. No need to duplicate this effort needlessly.
The trick here is to send STDERR to a logfile and to email, but not STDOUT. The following example illustrates how to do this.
Assume that my script executes the following command.
ls tmp/foo
is successful, so this output goes to STDOUT.ls tmp/bar
generates an error, so this output is sent to STDERR.The following cronjob will hide any STDOUT, but will redirect STDERR to /var/log/test.log
Here are the results. The email and ~/var/log/test.log will both say the same thing.
The email body says:
The logfile says the same thing:
As an added bonus, it is also possible to send STDERR & STDOUT to a logfile (which you only look at occasionally), but send STDERR to the screen (if run by hand) or email (if run from cron). I use the following snippit for long-running buildscripts.
Use
tee
:in your case:
Does this answer your question? https://stackoverflow.com/questions/1396506/cron-send-email-with-stderr-but-not-stdout
Cron is supposed to send an email after each run unless directed to
/dev/null