3

Since the % has special meaning to cron, how do I use it as part of a command?

I found this page http://www.hcidata.info/crontab.htm, which mentions piping echo and sed to escape the % and then remove the extra \, but in my case, the command is date, which doesn't accept parameters through STDIN, so this doesn't work:

echo '+\%Y' | sed -e 's|\\||g' | date

I'm thinking I can create a special date script that will output the date in the format I want, and call that instead, but I'm still wondering if it's possible to do it directly in the cron command.

Thanks!

Ivan
  • 395

1 Answers1

3

The crontab(5) man page says:

Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, [...]

so you should be able to just escape the % character by prefixing it with a backslash \:

00 15 * * *  user  date '+\%Y'

The \% sequence is processed by cron, so the shell will see the command date '+%Y' which is a correct invocation of date.

muru
  • 197,895
  • 55
  • 485
  • 740