How can I execute `date` inside of a cron tab job?

Short answer:

Escape the % as \%:

0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+\%d"`.log

Long answer:

The error message suggests that the shell which executes your command doesn't see the second back tick character:

/bin/sh: -c: line 0: unexpected EOF while looking for matching '`'

This is also confirmed by the second error message your received when you tried one of the other answers:

/bin/sh: -c: line 0: unexpected EOF while looking for matching ')'

The crontab manpage confirms that the command is read only up to the first unescaped % sign:

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 cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.


If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape % and DO NOT put it in $()

For example, while declare the string, just write:

DATEVAR=date +%Y%m%d_%H%M%S

Then, write cron statement with $($VARIABLE_NAME) like this:

* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log

Thanks to cyberx86, her/his answer at ServerFault might be more completed:


You can also put your commands into a shell file and then execute the shell file with cron.

jobs.sh

echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log

cron

0 * * * * sh jobs.sh