I have a crontab like this on a LAMP setup:

0 0 * * * /some/path/to/a/file.php > $HOME/cron.log 2>&1

This writes the output of the file to cron.log. However, when it runs again, it overwrites whatever was previously in the file.

How can I get cron to output to a file with a timestamp in its filename?

An example filename would be something like this: 2010-02-26-000000-cron.log

I don't really care about the format, as long as it has a timestamp of some kind.

Thanks in advance.

link|improve this question
1  
if you don't want $HOME/cron.log to be overwritten, use >> not > – Dave Cheney Feb 27 '10 at 8:01
feedback

2 Answers

up vote 3 down vote accepted

Try:

0 0 * * * /some/path/to/a/file.php > $HOME/`date +\%Y\%m\%d\%H\%M\%S`-cron.log 2>&1

Play around with the date format, if you like; just be sure to escape any % like \%, as above.

link|improve this answer
This is how I've done it in the past, works great. – ongle Feb 27 '10 at 1:42
feedback

You can also append your output to the log file by doing it like this:

0 0 * * * /some/path/to/a/file.php >> $HOME/cron.log 2>&1
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.