cron如何根据日期将其输出到新的日志文件? [英] How can cron output to a new log file based on date?

查看:195
本文介绍了cron如何根据日期将其输出到新的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将cron输出记录到带日期的文件中- /tmp/log/cron-2014-12-17.log

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(通过 root 用户)

Cron (via root user)

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

/ tmp / log 每分钟之后仍然为空。

/tmp/log remains empty after each minute.

如果我从命令行a手动运行脚本创建日志文件,并按预期输出。

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

此外,如果我创建文件并对其进行chmod 777处理,则cron 将输出写入此创建的文件中。它只是不会即时创建一个。

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

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

我丢失了什么?

推荐答案

这将解决您的问题:

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

这篇关于cron如何根据日期将其输出到新的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆