2

I'd like to log cron output to a dated file — /tmp/log/cron-2014-12-17.log

$ mkdir /tmp/log
$ chmod 777 /tmp/log
$ ls -lah /tmp/log
drwxrwxrwx 2 root root 4.0K Dec 17 21:51 .

Cron (via root user)

* * * * * /usr/bin/php /path/to/script.php > /tmp/log/cron-$(date "+%F").log 2>&1

/tmp/log remains empty after each minute.

If I run the script manually from command line a log file is created, and output is as expected.

// Running it manually as a CLI works fine, but not as a cron
$ /usr/bin/php /path/to/script.php > /tmp/log/cron-$(date "+%F").log 2>&1

Also, if I create a file and chmod 777 it, the cron will write output to this created file. It just won't create one on the fly.

// Let's create it first
$ touch /tmp/log/cron.log
$ chmod 777 /tmp/log/cron.log
// wait for the next minute...
$ tail -f /tmp/log/cron.log
output... output... output...

But this doesn't work for dynamic names like /tmp/log/cron-2014-12-17.log.

What am I missing?

Ryan
  • 14,682
  • 32
  • 106
  • 179
  • Possible duplicate of [Append current date to the filename via Cron?](https://stackoverflow.com/q/9110663/608639), [Sending cron output to a file with a timestamp in its name](https://serverfault.com/q/117360), [How to add the logs to a crontab with time stamp](https://askubuntu.com/q/391542), [How to log cron jobs?](https://stackoverflow.com/q/4811738/608639), etc. – jww Oct 30 '19 at 21:54

1 Answers1

8

This will fix your issue:

0 0 * * * /some/path/to/a/file.php >> /tmp/log/cron-`date +\%F`.log 2>&1
Ryan
  • 14,682
  • 32
  • 106
  • 179
Anish
  • 4,262
  • 6
  • 36
  • 58