Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

1  *    * * *   root    php /mix/dostuff.php > /tmp/dostuff.txt

Now, I want to make it to write instead of /tmp/dostuff.txt, to /tmp/dostuff-YYYY-MM-DD-HH-II-SS.txt

I could get the date part, using this:

TIMESTAMP=`date +%Y-%m-%H\ %k:%M:%S`

Now, how do I stick this TIMESTAMP into output file?

share|improve this question
up vote 1 down vote accepted

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
share|improve this answer
    
ow, I see. thank you for the enlightment. – ariefbayu Sep 9 '09 at 11:04
    
Beware %Y-%m-%H isn't the usual year-month-date combination - try %Y-%m-%d ! – shuckc Aug 1 '14 at 6:03
1  *    * * *   root    php /mix/dostuff.php > "/tmp/dostuff-`date +\%Y-\%m-\%H\ \%k:\%M:\%S`.txt"

Simple as that.

share|improve this answer
    
wont work, crontabs aren't evaluated in the same environment as a default shell. – John T Sep 9 '09 at 10:54
2  
I don't see how this would be a problem here. The cron environment is sufficient for this command. Besides, it works, I just tested it myself. – e-t172 Sep 9 '09 at 11:02
    
cron parses the file, not /bin/bash, thats why. – John T Sep 9 '09 at 11:07
2  
crontab(5): "The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file." You can even write entire shell scripts in a crontab line if you want! – e-t172 Sep 9 '09 at 11:12
1  
the space between date and + is very important, just for the next guy who comes along – jcollum Mar 15 '12 at 15:27

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
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .