Ubuntu – Command with percent symbols not running in crontab

cron

The following crontab does not work:

# TEST LINE DOES DOT RUN
*/1 * * * * /bin/echo 'test '`/bin/date +%Y-%m-%d` >> /tmp/test

I also tried starting it with:

SHELL=/bin/bash

Update: I thought the backtick characters ` were the villains, but as the answer below clarifies the percentage % was the culprit!

Best Answer

In /bin/date +%Y-%m-%d, you need to escape each % with \ according to this man page:

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.

Related Question