Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

php - How can cron output to a new log file based on date?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will fix your issue:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...