my goal is to save cronjob error and email it to me. I don't care about standard output, that's why I redirect it to /dev/null.
Sending mail is done by this setting in crontab:
MAILTO=my@email.com
I tried to run it with following command:
* * * * * /path/to/script.sh > /dev/null 2 >> /path/to/file.log
It emails the error all right. The log file is created but is empty.
What am I doing wrong?
==================================================================================
I found the solution. Thank you all for your help! (I cannot post it as an answer, so I am amending the solution here.)
I used this reference How can I redirect stderr to a pipe?
# version 2: redirect stderr to the pipe without getting stdout (it's
# redirected to /dev/null)
myprog 2>&1 >/dev/null | grep ...
My solution is:
#crontab
MAILTO=my@email.com
* * * * * /path/to/script.sh 2>&1 >/dev/null | tee -a /path/to/file.log
Explanation: redirecting stderr to the pipe without getting stdout and then using tee -a for appending the stderr to the log file and printing it to the terminal, which is redirected automatically to email (see MAILTO).
So I have a log file and email both containing the error message.
crontab
needs the full path of the scripts to be executed. Try with* * * * * /bin/sh /path/to/script.sh ...