Crontab Command Substitution not Working – Solution

By default crontab sends the cron job output to /var/spool/mail/{username} but this time I wanted one of my daily cron job to write to a different log file each day. So I changed it to something like:

30 2 * * * &> /.../`date +%F`.log

But the next day I found that the script didn’t run at all. I tried to copy the command and execute it directly in shell – it worked. Then I looked at /var/spool/mail/{my username} and found the following error message:

/bin/sh: -c: line 0: unexpected EOF while looking for matching “’
/bin/sh: -c: line 1: syntax error: unexpected end of file

At last I found that it was because of the “%” character. Percent symbol has special meanings in crontab. Escaping it with backslash solves the problem:

30 2 * * * &> /.../`date +\%F`.log

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your intelligence before hitting * Time limit is exhausted. Please reload CAPTCHA.