102

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?

157

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.

  • Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png – Tebe May 20 '15 at 6:24
  • 2
    @Копать_Шо_я_нашел cron will send an email with the error message, – Jasen Jan 1 '16 at 6:50
  • 3
    date +\%Y\ \%m\ \%d\ \%H:\%M:\%S-cronlog – DevilCode Apr 4 '16 at 13:36
7

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
6

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:

2

In cron, you can use this simple syntax:

*/15 01-09 * * * sh /script.sh >> /home/username/cron_$(date -d"-0 days" +\%Y\%m\%d).log 2>&1
  • Output date format will retrun like cron_20180123.log – bala4rtraining Jan 24 '18 at 13:51
  • (1) What are you saying that hasn’t already been said by the accepted answer?   (2) Your answer is much more complicated than the question.  For example, you added the -d option, which is not used in the question (and you did not explain it).  How do you justify calling this “simple syntax”? – G-Man Dec 23 '18 at 6:27
2

All of the above answers use double quotes (not all of them worked for my setup). This worked for me:

0 5 * * 3 /data/script.sh > /data/script_`date +\%y\%m\%d`.log 2>&1
  • 1
    What are you saying that hasn’t already been said by the accepted answer? Are you saying that it works better without quotes than it does with quotes? (Hint: that’s very unlikely.) – G-Man Dec 23 '18 at 6:27
  • The accepted answer simply doesn't work for me. This one does. – Manuel Schmitzberger Dec 23 '18 at 8:44

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.