Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Hey, how about that, it works... The page I referenced confused me then. –  Ivan Oct 12 '10 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.