Home » Questions » Computers [ Ask a new question ]

How can I add a timestamp to the end of a cron job's output file name?

How can I add a timestamp to the end of a cron job's output file name?

I want to track each crontab output to separated file. Normally, I use this as crontab entry:

Asked by: Guest | Views: 91
Total answers/comments: 2
Guest [Entry]

"crontabs are run in a different environment than the default shell, so you can use a small shell script under your home directory with something like this in it (we'll name it script.sh):

#!/bin/bash
php /mix/dostuff.php > /tmp/dostuff-`date +%Y-%m-%H\ %k:%M:%S`.txt
exit

then change this scripts permissions to ensure it is executable, 755 should be fine. Then in your crontab:

1 * * * * root /home/yourusername/script.sh"
Guest [Entry]

"On CentOS 6.6, I use

*/10 * * * * /usr/bin/php -q /var/www/script.php > cron-job-log-$(date +\%Y\%m\%d\%H\%M\%S).txt

it will create a file every 10 min

cron-job-log-20150401124001.txt"