I was playing aroudn with some variations of date like

DATE = $(date)

but that didnt work either

crontab -e

DATEVAR=`date +20\%y\%m\%d_\%H\%M\%S`
* * * * * echo $DATEVAR >> /tmp/log/crontab.log
*/2 * * * * echo "$DATEVAR hello" >> ${CRONLOG}
*/1 * * * * echo 'every minute' >> ${CRONLOG}

this just outputs the text as is...

I want to create a log entry in crontab.log with a timestamp on each update

How can I do this on CentOS 6?


DATEVAR=date +20%y%m%d_%H%M%S
*/1 * * * * /bin/echo [CRON] $($(DATEVAR)) >> /tmp/log/crontab.log

rendered only [CRON] and NOTHING when I tried it =/


Cron is not a shell - it does not parse commands in the same way that a shell does. As such, your variable is assigned as if it was static text.

There are three solutions I know of to this problem:

Option 1: Use a shell script to generate your command, include whatever variables and logic you want - and call that shell script from cron.

* * * * * /path/to/myscript.sh


DATEVAR=`date +20\%y\%m\%d_\%H\%M\%S`
echo $DATEVAR >> /tmp/crontab.log

Option 2: Include the date command directly in your command, and, since the entire command is passed to the shell, the date will be processed and replaced with an actual date.

* * * * * /bin/echo `date +20\%y\%m\%d_\%H\%M\%S` >> /tmp/crontab.log

Option 3: Set the string variable in cron, and pass that to your command to be processed (note - the percent signs do not need to be escaped, and the variable itself is wrapped in $() to execute it in a separate shell - backticks should work the same):

DATEVAR=date +20%y%m%d_%H%M%S
* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log

(In all the cases above, you can, of course, use a variable for the log path, instead of 'hard coding' it.)

  • 1
    thanks for the great feedback +1, i tried to add it to the crontab but doing that didnt work =/ it never renders DATEVAR – qodeninja Dec 11 '11 at 1:50
  • I just did this on a RHEL/CentOS6 compatible system and it gave the expected result (i.e. added a new line to the file with the date). Which option (of the 3 above) did you try, and what was the result - be specific - and was there an error in /var/log/cron? (if you use the script option, a) don't forget to chmod +x and b) try it on its own first (i.e. not through cron)) – cyberx86 Dec 11 '11 at 1:53
  • 1
    option 3 =], I had tried to do that before but it didnt render. But now im noticing you did something a lil dif. why did you double up the $($(DATE))? see my update above – qodeninja Dec 11 '11 at 1:57
  • 1
    You have a typo in the update you added - it is $($DATEVAR) not $($(DATEVAR)). Think of it this way - $() - that is, the outer bracket - launches a new shell to process whatever is in the brackets. In this case, the variable $DATEVAR is set to a string and is passed to the shell. $(DATEVAR) is nothing - because DATEVAR is nothing (i.e. is it missing a $ to identify it as a variable). You could also use backticks around $DATEVAR to get the same result – cyberx86 Dec 11 '11 at 2:01
  • fair enough. updated it. lets try this bad boy! -- Hazaaah! it worked lol – qodeninja Dec 11 '11 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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