I have a crontab that looks something like this:

 0 8 * * * command >> mylog.log 2>&1

This appends both errors and standard output into mylog.log.

In addition to what's above, I want to be able to capture only the most recent outputs of command and send them to my email via:

 mail -s 'test' [email protected]

In other words, write all outputs to a log, and send only new outputs to an email.

I read up on tee, and something tells me that may be an answer but I'm not 100% sure on that.

1 Answer 1


After checking with a colleague, the solution ended up being incredibly simple. Just divide out the script into multiple parts. Put these parts into a shell script.

 # write truncated log to temp.log 
 command > temp.log 2>&1
 # mail contents of truncated log
 mail -s 'test' [email protected] < temp.log
 # append contents of truncated log to your primary log
 cat temp.log >> mylog.log

Then take the bash script and run it in crontab

 0 8 * * * bash mysh.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .