Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment
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
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
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
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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