What's wrong with my cronjob syntax, I'm trying to use a backtick (`)?

Solution 1:

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 [email protected] -s "query for `date +\%Y-\%m-\%dZ\%I:\%M`"

Solution 2:

To resolve the issue escape your % characters. date +%Y-%m-%d should be date +\%Y-\%m-\%d

Unfortunately this answer is a little late but the issues is not PATH or backticks - the issue is simply that the percent character '%' is a special character used to denote a NEWLINE or an STDIN in crontab entries.

This conflicts with the formatting input of the date command. As such a command that includes: date +%Y-%m-%d will be interpreted as:

date Y- m- d

Solution 3:

I had a lot of problems with backticks also. Sometimes you need more than one occurrence of quotes and backticks. Just replace them for $().


export NOW=`date`
export NOW=$(date)

-Gilson Soares