How can cron output to a new log file based on dat

2020-02-15 03:16发布

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?

1条回答
祖国的老花朵
2楼-- · 2020-02-15 03:54

This will fix your issue:

0 0 * * * /some/path/to/a/file.php >> /tmp/log/cron-`date +\%F`.log 2>&1
查看更多
登录 后发表回答