本文介绍了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 每分钟后保持为空.
如果我从命令行手动运行脚本,则会创建一个日志文件,并且输出符合预期.
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 之类的动态名称.
But this doesn't work for dynamic names like /tmp/log/cron-2014-12-17.log.
我错过了什么?
推荐答案
这将解决您的问题:
0 0 * * * /some/path/to/a/file.php >> /tmp/log/cron-`date +\%F`.log 2>&1
这篇关于cron 如何根据日期输出到新的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)