Monday, January 18, 2010

Backticks and Percent Signs in Crontab

I setup a cron job recently and got a strange error message emailed to me even though I had tested the command out beforehand. I initially though it had to do with using backticks, but it turns out it had to do with unescaped percent symbols.

It is possible to use backtick's in the crontab, however if you use them in a statement like this:
0 * * * * perl /tmp/`date %Y-%m-%d`.csv

You will get a very cryptic error message. The reason for this is the % character has special meaning in the crontab and must be escaped to work properly. So the above command would look like:
0 * * * * perl /tmp/`date \%Y-\%m-\%d`.csv

No comments:

Post a Comment