crontab 脚本失败:调用 $(date) 时文件意外结束(预期为“)”)

crontab script fail: end of file unexpected (expecting ")") when call $(date)

我要补充

0 5 1 * * goaccess -f /var/log/nginx/access.log -a > /home/xan/reports/report-week-$(date +%Y.%m.%d).html

但是 crontab 总是抱怨:

Subject: Cron <root@deimos> goaccess -f /var/log/nginx/access.log -a > /home/xan/reports/report-week-$(date +
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <E1bIogT-0001FX-9n@deimos>
Date: Fri, 01 Jul 2016 05:00:01 +0200

/bin/sh: 1: Syntax error: end of file unexpected (expecting ")")

执行此操作的正确语法是什么?

%在crontab中有特殊含义(代表换行符),所以你需要转义它来指定文字百分号。

 0 5 1 * * goaccess ... > /home/xan/reports/report-week-$(date +\%Y.\%m.\%d).html