How to send email and save log file from one cronjob?

I have a cronjob:

* * * * * root echo 'blabla' 

This is easy :)

Now I would like to send an email when this cronjob is completed, but also save the log in the log file.

I tried this:

 * * * * * root echo 'blabla' | mail -s "Cron report" test@example.com > /test/test.log 2>&1 

An email is sent and the test.log file is created, but the test.log file is empty.

Any idea why?

+4
source share
1 answer

This is because you redirect echo output to mail , so there is nothing to write to the log file. As a result, the log file is empty.

If you want to write echo output to a log file and also send it to mail , use tee , as shown below:

 echo 'blabla' 2>&1 | tee /test/test.log | mail -s "Cron report" test@example.com 
+5
source

Source: https://habr.com/ru/post/1492649/


All Articles