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?

link|improve this question

feedback

2 Answers

up vote 0 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
link|improve this answer
ow, I see. thank you for the enlightment. – silent Sep 9 '09 at 11:04
feedback
1  *    * * *   root    php /mix/dostuff.php > "/tmp/dostuff-`date +\%Y-\%m-\%H\ \%k:\%M:\%S`.txt"

Simple as that.

link|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
1  
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
1  
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
the space between date and + is very important, just for the next guy who comes along – jcollum Mar 15 at 15:27
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.