【问题标题】:How can cron output to a new log file based on date?cron 如何根据日期输出到新的日志文件?
【发布时间】:2023-03-09 04:12:01
【问题描述】:

我想将 cron 输出记录到一个过时的文件中 — /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(通过 root 用户)

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

/tmp/log 每分钟后都为空。

如果我从命令行手动运行脚本,则会创建一个日志文件,并且输出符合预期。

// 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

另外,如果我创建一个文件并 chmod 777 它,cron 将 将输出写入这个创建的文件。它只是不会即时创建一个。

// 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...

但这不适用于像 /tmp/log/cron-2014-12-17.log 这样的动态名称。

我错过了什么?

【问题讨论】:

标签: php cron file-permissions


【解决方案1】:

这将解决您的问题:

0 0 * * * /some/path/to/a/file.php >> /tmp/log/cron-`date +\%F`.log 2>&1

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 2020-07-13
    • 2012-04-21
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    相关资源
    最近更新 更多