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

I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:

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

Unfortunately I get this message when that runs:

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file

I have tried escaping the date part in various ways, but without much luck. Is it possible to make this happen in-line in a crontab file or do I need to create a shell script to do this?

Asked By: cwd


Short answer:

Escape the % as %:

0 * * * * echo hello >> ~/cron-logs/hourly/"test$(date +%d).log"

This also uses $(...) instead of the deprecated `...` syntax for command substitution and quotes the expansion of said command substitution.

Long answer:

The error message suggests that the shell which executes your command doesn’t see the second backtick character:

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

This is also confirmed by the second error message you 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.

Answered By: Adam Zalcman

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

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


0 * * * * sh
Answered By: Trevi Awater

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:

Answered By: Gawi – Kai

In cron, you can use this simple syntax:

*/15 01-09 * * * sh / >> /home/username/cron_$(date -d"-0 days" +%Y%m%d).log 2>&1
Answered By: bala4rtraining

This worked for me:

0 5 * * 3 /data/ > /data/script_`date +%y%m%d`.log 2>&1
Answered By: Manuel Schmitzberger

A basic solution:

  • use $() for executing date command and return output
  • format datetime to UTC, escape the % character with
  • add 2>&1 at the end for streaming both stdout and stderr into that log file


* * * * * echo "Test crontab log" > /tmp/crontab.log.$(date --utc +%Y%m%d_%H%M%SZ) 2>&1


ls -lh /tmp | grep log

-rw-rw-r-- 1 ubuntu  ubuntu    17 May  4 05:06 crontab.log.20190504_050601Z
-rw-rw-r-- 1 ubuntu  ubuntu    17 May  4 05:07 crontab.log.20190504_050701Z
Answered By: Hieu Huynh
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.