Cron – Fixing Syntax Issues with Backticks

cronsyntax

Here's what I'd like to automate:

00 08 * * * psql -Uuser database < query.sql | mail somone@null.com -s "query for `date +%Y-%m-%dZ%I:%M`"

Here's the error message:

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

Best Answer

From crontab(5):

The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell's trailing "\".

Just add backslashes before % signs:

00 08 * * * psql -Uuser database < query.sql | mail somone@null.com -s "query for `date +\%Y-\%m-\%dZ\%I:\%M`"
Related Topic