Issue
I've got a cron job I want to execute a PHP file, then take the output and both email and save to a log file.
I can use this to email:
/path/to/php /path/to/script.php | mail -s "script results" [email protected]
And I can use this to save to a log file:
/path/to/php /path/to/script.php >> /path/to/logfile.log 2>&1
But this doesn't work for me as I expected:
/path/to/php /path/to/script.php | mail -s "script results" [email protected] >> /path/to/logfile.log 2>&1
It appears to be emailing the output, but not writing to the log file.
I'm running Ubuntu 16.04.
Solution
What you are looking for is tee
command.
You can do it like this
/path/to/php /path/to/script.php 2>&1 | tee -a /path/to/logfile.log | mail -s "script results" [email protected]
Answered By - Michal HynĨica